The PATH Variable
The PATH variable is one of the most important environment variables. It tells the shell where to find executable programs.
What is PATH?
PATH is a colon-separated list of directories where the shell looks for commands:
echo $PATH
# /usr/local/bin:/usr/bin:/bin
How PATH Works
When you type a command:
- Shell checks if it's a built-in
- Shell searches PATH directories (left to right)
- First match is executed
- If not found: "command not found"
Viewing PATH
echo $PATH
Make it readable:
echo $PATH | tr ':' '\n'
Common PATH Directories
| Directory | Contains |
|---|---|
/usr/local/bin | Locally installed software |
/usr/bin | Standard system commands |
/bin | Essential system commands |
/sbin | System administration commands |
~/bin or ~/.local/bin | User's personal scripts |
Exercise: View Your PATH
Display the PATH variable:
Adding to PATH
Temporarily (Current Session)
export PATH=$PATH:/new/directory
export PATH=/new/directory:$PATH # Add to front
Permanently
Add to ~/.bashrc:
echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
source ~/.bashrc # Reload
Order Matters
Directories are searched left to right. First match wins:
# If /usr/local/bin is first, its python runs
PATH=/usr/local/bin:/usr/bin
# If /usr/bin is first, system python runs
PATH=/usr/bin:/usr/local/bin
Finding Commands in PATH
which python # Shows which python runs
type python # Shows command type and location
Common PATH Modifications
Add Local Scripts
export PATH="$HOME/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"
Add Programming Tools
# Node.js
export PATH="$HOME/.npm-global/bin:$PATH"
# Go
export PATH="$PATH:/usr/local/go/bin"
# Rust
export PATH="$HOME/.cargo/bin:$PATH"
Troubleshooting PATH Issues
Command Not Found
# Check if command exists
which mycommand
# If not found, check where it is
find /usr -name mycommand
# Add its directory to PATH
Wrong Version Running
# See which one runs
which python
# Check all locations
type -a python
# Reorder PATH if needed
Exercise: Modify PATH
Add a directory to your PATH:
Security Note
Never put . (current directory) at the start of PATH!
# DANGEROUS - allows hijacking
PATH=.:$PATH
# An attacker could put a malicious 'ls' in any directory
Key Takeaways
- PATH tells shell where to find commands
- Directories separated by colons
- Search happens left to right
- Add directories with
export PATH=$PATH:/new/dir - Make permanent by adding to
~/.bashrc

