Understanding $PATH

Open your terminal and type in

echo $PATH

and you’ll see an output that might look something like

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Each of those paths (separated by a colon) points to a directory that contains executables. All of those directory locations are part of the user PATH file- a grouping of places where executables can be kept. If a file called “node” is contained in one of those folders, typing “node” into the terminal will immediately look in the user path folders for a file called “node” - this itself might be an executable or might be a symbolic link to an executable located elsewhere. Either way, the ability to just type a single command and execute a program is due to the PATH file.

The PATH file itself is just a list of directories that tell the kernel where to look for the executable. There is an order in which the paths are checked for that executable - it’s the same order that the paths are printed to the console.

If you use ls -l to list all of the contents of one of the PATH directories, you’ll see some interesting stuff.

Anything prefixed with an “l” on the left side of the output is a symlink to an executable. There are probably files int here that are just a word as the filename - no extension, no path associated with it. These are pure scripts. If you open any of these pure scripts up, you’ll see a shebang at the top of the script. The shebang is the next thing we’ll cover here.

A shebang at the top of a script looks something like this - 

#!/usr/bin/env python

This line tells the kernel where to find the interpreter that will be used to interpret the lines of code contained in the rest of the script. It’s also possible to use 

#!/usr/bin/python

However, if this script is running on a different machine and the python executable isn’t found in /usr/bin/python, the script won’t run. “Env” is a script itself that acts much as the shell would - it will search the directories loaded in the PATH file until it finds the interpreter specified. Including the “env python” makes the script more portable.

If you use VS Code, there is a feature to add the executable path to the user PATH file with a single click - it simply adds a symlink called “code” to  one of the directories listed in the PATH file which leads to the actual executable file. To prove this, after adding the “code” command to the PATH file (using VS Code, not doing it manually), type in 

which code

in terminal to see the location of that command (which will be a directory that is listed in our PATH file). Open that folder and you’ll see an icon called “code” with an arrow pointing out - a symlink that leads to the VS Code IDE executable. That’s how we can now type “code” and launch our editor. 

To edit the PATH file and add a new directory path to it, we can type

$ export PATH=$PATH:/path/to/directory

Note that this is different from

$ export PATH=/path/to/directory:$PATH

The former will add the new directory to the bottom of the list so it will be checked last when searching for an executable - the latter will add the new directory to the beginning of the list so it’ll be checked first. *THIS WILL NOT PERSIST CHANGES TO THE PATH FILE*. In order to globally change the directories listed in the PATH file, the users ~./bash_profile or ~/.bashrc file must be edited directly.

This is all important to keep in mind when either scripting or building a CLI tool. More on that later.