[Bash] Bash PATH Environment Variable

~ $ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/aamnah/Dropbox/todo

~ $ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

PATH=/usr/local/bin:$PATH # prepend a path to your current $PATH

Ubuntu

The following command adds (appends) a path to your current path:

export PATH=$PATH:/my/custom/path

If you want your setup to execute this command every time, there are a number of places where you can put it. When you login, the following scripts will be executed in this order:

/etc/profile      (which starts by loading everything in /etc/profile.d)
~/.profile        (which starts by loading ~/.bashrc if you are running bash)

To overwrite an existing PATH, use the same export command without the $PATH included in the value. Like so:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

: is the seperator between different PATH locations.

To find out if a command is in the path, use which. For example:

which node

on a Mac will give you

/usr/local/bin/node

If you get nothing from the which node coomand, node’s not in the path.

Notes

  • ~/.profile is only loaded if ~/.bash_profile or ~/.bash_login DO NOT EXIST. Otherwise, at least bash, will load them instead. It is suggest to use .profile and not the bash specific scripts. So, if in these attempts you created .bash_login, please delete it now.

  • ~/.bashrc is only loaded if you are running an interactive session. (something with a prompt where you can actually type something.

  • ~/.bashrc is loaded again and again, everytime you open up a new terminal. So a new tab in gnome-terminal, a new virtual terminal, etc. So even if you don’t login again, .bashrc is loaded (and thereby resets its environment) everytime you open a new shell.

  • Something like byobu should really go into .profile, (otherwise it won’t work ;-)

  • Something like paths should go into .profile if you want it to work outside of the interactive sessions. (say when you press alt+f2 in gnome)

Mac OS X

Check what current $PATH is:

echo $PATH

OR

printf "%s\n" $PATH

You can add path to any one of the following method:

  • $HOME/.bash_profile file using export syntax. e.g. export PATH=$PATH:/new/dir/location1
  • /etc/paths.d directory.