Everything You Need to Know about $PATH in Bash



# Where do Bash look for commands?
echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

# Where is sudo came from?
which sudo
# /usr/bin/sudo



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Prepending and Appending to $Path



$ PATH=$PATH:/some/dir          # Append - Correct
$ PATH=/root:$PATH              # Prepend - Not preferred


• Because of the possible injection of evil script, use absolute (full) path in script files

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Where is the original $PATH came from?



less /etc/profile
less /etc/profile.d/


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Customizing $PATH in ~/.bash_profile (for a single user

)

# Add inside ~/.bash_profile
PATH=$PATH:/$HOME/bin


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Interactive Login Shells (except root)



# Insert code just before "export PATH" in /etc/profile
PATH=$PATH:$HOME/bin


Index