What Is the Unix PATH, and How Do You Add Programs To it?

The UNIX path is an environment variable that tells the shell which directories to look for when you issue commands. You can think of it as a list of Application or Program Files.

What does PATH do?

If you want to run a binary in your current folder, you need to provide a relative path, like this:

./command [args]

But, if you move this binary to a folder like /usr/local/bin/, you simply type the name of the command to execute it:

command [args]

This is due to the fact /usr/local/bin/ is on the WAY. The shell can look in this folder if you don’t specify a location. Many command line utilities are installed here and in similar folders. You can see which files are on your PATH by echoing the variable itself, using the $ prefix:

echo $PATH

By default, however, the PATH variable is a list of directory names divided by a colon. It is not very legible:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

But, with a little regex, you can clean it to print a list:

echo $PATH | sed $'s/:/\n/g'

These directories all have different objectives:

  • Files ending in bin are for general use, files ending in sbin are for system administration commands, things the system does, but you probably won’t interact with much. Although there are
  • /bin and /sbin are for programs that the system needs before /usr is available. This is where key programs like sh and ls are stored.
  • /usr/bin and /usr/sbin are intended for normal user programs, usually integrated into the operating system.
  • /usr/local/bin and /usr/local/sbin are for programs that you downloaded or compiled yourself. If you have a binary that you downloaded and you don’t know where to put it, drag it into /usr/local/bin.

/usr/local/sbin may not even exist on your system because it is quite contradictory in nature. It is not present on macOS, but contains some programs on Ubuntu.

You can see in which folders the different commands are located with the which order:

which ls

Which will display the full path to the given command.

How to add folders to PATH

Adding a folder to PATH is simple, you just need to define the variable. But you cannot completely overwrite it, so you add a folder to the front of the PATH:

PATH=~/folder:$PATH

You can also nail the file to the back of the PATH:

PATH=$PATH:~/folder

These have different uses. Files close to the front will be checked first, so if there are duplicate commands (for example, two versions of Python, both called “python“) Adding the folder to the front will ensure that the folder you added manually will be checked first. Most of the time, that’s probably what you want.

Usually you define this in your shell profile, which will define the variable each time you open a new terminal. If you use bash, it’s either .bash_profile or .bashrc. If you use a shell like zsh, it will be a different file (.zshrc in that case). If a definition already exists, you can simply add your file to the start of the list, separated by a colon.

Consider using symbolic links

Rather than adding a folder directly, you can instead use a symbolic link to specific commands and place the symbolic link in/usr/local/bin. For example, Python installs in its own folder, but rather than adding this folder to PATH, it uses a symbolic link. You can see where the symbolic links lead ls -la:

ls -la $(which python)

And you can create symbolic links with ln:

ln -s command /usr/local/bin/command

PATH can change

Since the PATH variable is usually defined in .bashrc, your PATH may be different when running in a different environment. This is particularly relevant if you run in a bash script or if you run a command via cron. If you encounter problems of type “command not found”, you must check that the PATH is correctly defined in the environment in which you work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.