Adding Directories to PATH

In Linux machines, executable files are usually stored in /usr/bin, /usr/local/bin, or similar system-wide directories, which means that in order to install software on those directories, one needs administrator privileges. However, often times we need to install programs without such privileges, so we need a way of telling the operating system to look for programs in other directories (that we can write to as regular users).

That is where "PATH" comes in. "PATH" is a variable that has a colon-separated list of directories in which the operating system is going to look for executable files. Such variable can be specified in two places.

  • In a bash terminal: /home/username/.bashrc (where "username" is your home directory). Actually, all the commands inside that file are going to be run every time you open a terminal, so by storing your PATH in .bashrc, you can rest assured that your PATH will be recognized in your interactive session.
  • Inside a bash script. Especially when running jobs on a shared server, it is better to include all your custom PATHs in each script, to make sure that the cluster identifies the correct PATH. This is recommended instead of including all your PATHs in .bashrc because sometimes the cluster does not export your variables with your jobs
So, in any of the files mentioned above, you will type
      export PATH="/path/to/directory":$PATH
    

The export part, makes sure the PATH variable gets used by all the commands from now on. Then, you type the path to the directory containing your installed programs in double quotes, and finally, you append $PATH with a ':', so that you also include the directories previously in you PATH.

Bonus tip!: Execute echo $PATH | tr ":" "\n" in a terminal to see the all the directories in your PATH, one per line