Skip to main content
p. 23

2.9 The Command Path

PATH is a special environment variable that contains the command path (or path for short), a list of system directories that the shell searches when trying to locate a command. For example, when you run ls, the shell searches the directories listed in PATH for the ls program. If programs with the same name appear in several directories in the path, the shell runs the first matching program.

If you run echo $PATH, you’ll see that the path components are separated by colons (:). For example:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin
p. 24

To tell the shell to look in more places for programs, change the PATH environment variable. For example, by using this command, you can add a directory dir to the beginning of the path so that the shell looks in dir before looking in any of the other PATH directories:

$ PATH=dir:$PATH

Or you can append a directory name to the end of the PATH variable, causing the shell to look in dir last:

$ PATH=$PATH:dir
note

You can accidentally wipe out your entire path if you mistype $PATH when modifying your path. If this happens, don’t panic! The damage isn’t permanent; you can just start a new shell. (For a lasting effect, you need to mistype it when editing a certain configuration file, and even then it isn’t difficult to rectify.) The easiest way to return to normal is to close the current terminal window and start another.