Everyone should know, but no one ever teaches it to you.

In my experience, the most common errors I come across when working on the terminal, or when my students work on the terminal, has to do with the path. What is this mysterious path and why is it important?? Why can’t the computer just know where to look for things????!

The computer is stupid. You have to tell it specific instructions for everything. When you run a program on the terminal, it will only look for programs/files that you need in the current directory you are in… plus the PATH!

Written as catered to users on Unix-based operating systems, such as Mac OS

What is PATH

Files on a computer are organized in a hierarchical folder system. The tilde symbol, ~, represents your home directory, or the uppermost folder on the system. Each file or folder on a computer has an address, or the location of it within the computer, represented by its path. For example, take the path: ~/Documents/cats.txt This means that the file “cats.txt” is located in the Documents folder, which is located in your Home directory.

PATH is an environmental variable on most operating systems that stores the locations of programs to be executed. The information contained in an environmental variable can be accessed when a process/program is executed. At the time of a program execution, if the full location of the files being used are not specified, the computer will check the PATH variable for locations.

The PATH is like an index to find where things are located.

What does PATH look like

It’s ugly. Let’s take a look at what your computer’s PATH looks like. Type this into your terminal:

echo $PATH

This is what mine looks like:

path

Basically the addresses are each separated by a colon. When a computer checks your PATH, it will iterate through each specified address on the PATH. A more clean view:

echo $PATH | tr \: \\n

How to edit PATH

When installing software/programs, many of them automatically add the location to the files onto your path, so you do not have to do so.

1. From command line (Temporary)

To add a path to the PATH variable, use the export command. This will append your new path to the existing PATH variable. However, these changes ARE NOT PERMANENT. The path will be removed when you exit the terminal.

export PATH=$PATH:~/path/to/add

2. Add to login profile

If you add to your path via a login profile, this change is permanent, since these will be executed upon terminal start-up. Typically they are added to the ~/.bash_profile. Edit your .bash_profile and add the PATH command:

echo PATH=$PATH:~/desired/path:/another/path/to/add:/yet/another/path/to/add

path_helper on MacOSX

For Mac users, there is a path_helper utility tool to help manage the PATH variable. The paths that this utility uses can be found in /etc/paths

less /etc/paths

It should look similar to this, each addressed on its own line:

/usr/local/bin  
/usr/bin  
/bin

When run, the tool will read files in /etc/paths.d and append them to the PATH variable. Therefore, if you edit this /etc/paths file, you can add addresses to the PATH.

Sometimes when you install something, the installation did not automatically put its location in your PATH. Then, when you try to run that program, the computer can’t find it!

Sometimes executables require dependencies. When you run the executable, it can’t find them! Certain programs/scripts utilize other programs/scripts when run. For example, I have a program called CAT-CARE, which I installed, but when I try to run CAT-CARE, it says it needs CAT-FOOD and CAT-TOY. Therefore, you will need to instruct the computer where to find CAT-FOOD and CAT-TOY. One way would be to add the location in your PATH.

Conclusion

My first summer of learning to work on the terminal was unbearably grueling. If you’ve never worked on this type of system before, troubleshooting can be tedious since you aren’t aware of its basic logic rules. Understanding the PATH and how to use the PATH is one of the foundations of using a terminal!

Happy coding!

-Meshell

Tags:

Updated: