Linux / Bash
Beginner
1 min read
The PATH Variable and source
Example
# Display current PATH
echo $PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Find where a command lives
which python3
# /usr/bin/python3
which -a python3 # Show all matches in PATH
type python3 # Also shows aliases, builtins
command -v git # Portable way to check if command exists
# Add a directory to PATH (current session only)
export PATH="$PATH:/opt/myapp/bin"
# Prepend (higher priority)
export PATH="/opt/myapp/bin:$PATH"
# Add to PATH permanently (append to ~/.bashrc)
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
# Reload ~/.bashrc in current session
source ~/.bashrc
# Equivalent shorthand
. ~/.bashrc
# Create a local bin directory and add to PATH
mkdir -p ~/.local/bin
export PATH="$HOME/.local/bin:$PATH"
# Install a script to local bin
cp myscript.sh ~/.local/bin/myscript
chmod +x ~/.bashrc/myscript
# Source a .env file
# cat .env:
# export DB_HOST=localhost
# export DB_PASS=secret
source .env
echo $DB_HOST # localhost
# Inspect all shell startup file locations
# /etc/profile login, all users
# /etc/bash.bashrc interactive, all users
# ~/.bash_profile login, current user
# ~/.bashrc interactive, current user
# ~/.bash_logout on logout
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash