IT lingo breakdown: What is PATH?

A blurry screenshot of a terminal, overlayed by the text "echo $PATH"
Published: Feb 28, 2021
Last edit: Feb 28, 2021

PATH is one of the early stepping stones when you are just getting into programming. It is used ubiquitously and referred to frequently in tutorials or on StackOverflow. It is one of the things in your OS that you might never consciously need as a casual user, but as soon as you get into coding, it’s just everywhere.

So, what is PATH?

PATH is an environment variable. That means, it can affect the processes that run on your computer. Environment variables are values that many of the processes running in the environment will look up at some point of their runtime.

A simple one would be HOME. This is the environment variable that holds the path to your home directory, e.g. /Users/your_name in MacOS, /home/your_name in Ubuntu, or, under Windows C:\User\your_name.

Now PATH is only slightly more complicated than that. PATH actually holds many individual paths and everything contained in PATH will be executable from your command line from any directory. Imagine you wrote a script, and it is located in /Users/your_name/scripts/example.sh. Now, you could navigate to the scripts directory and execute example.sh. Alternatively, if this is something you need frequently or in specific locations, you could add /Users/your_name/scripts to PATH, and then you could execute example.sh from any directory on your machine.

PATH in practice

The following code is meant for UNIX-based systems. On Windows, PATH follows the same logic, but viewing/editing it is best done through the GUI. The way to find it is: Control Panel > System and Security > System > Advanced system settings > Advanced > Environment Variables. You will get a window listing all environment variables with buttons to add to or delete from them. It should be self-explanatory from there.

However, let’s get back to UNIX systems.

Inspecting PATH

To learn what is in your PATH, open a terminal and type

echo $PATH

You may want to inspect a few other environment variables while we are at it. We already had HOME and then there are e.g. TMPDIR (directory for temporary storage), and SHELL (the shell used by the current terminal). These might be named slightly different based on your UNIX flavor. To show all environment variables, type env.

Edit PATH (or other environment variables)

To change environment variables, you will want to use export. Like this:

export PATH="$/user/your_name/scripts:$PATH"

This adds the directory where our script is located to the beginning of PATH. However, since this change is not persistent, i.e. it will be gone as soon as you close the current terminal, you want to add this line to your .bash_profile or .bashrc file (assuming you use bash as your shell). These files are located in your home directory and are read every time you start a new shell. If you use a different shell, you will have analogous files going under the name of your shell.

You can now call your script example.sh from anywhere on your computer. To avoid overloading your PATH with too many paths, it would be a good idea to have a directory where you store all of those little scripts that you need frequently. This way you only need to change PATH once and future scripts you place in your script directory will automatically be detected. A conventional choice would be to use the directory $HOME/bin as your script directory.

Recap

  • PATH is a list of directories
  • Executable scripts inside these directories can be called with only their name from anywhere on the system
  • To edit PATH or other environment variables on UNIX, use the export command
  • Don’t overwrite PATH, append it using a colon : as separator

Found that interesting?

Share your experiences with me!

Contact