1.9 The Command Path

1.9 The Command Path

PATH is a special environment variable containing the command path (or path for short). A command path is a list of system directories that the shell looks in when trying to locate a command. For example, if you try to run the ls command, 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 program that matches.

If you run echo $PATH, you'll see that the path components are separated by colons (:). Here's a simple example:

/usr/local/bin:/usr/X11R6/bin:/usr/bin:/bin

To make the shell look in more places for programs, you can change the PATH environment variable. 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 with this command:

PATH=dir:$PATH

As an alternative, 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?

Exercise caution when modifying the path, because you can accidentally wipe out your entire path if you mistype $PATH. Don't panic if this happens, because it isn't permanent (for a lasting effect, you need to mistype it when editing a certain configuration file, and even then it isn't difficult to rectify). One of the easiest methods to get back to normal is to exit the terminal window that you're using and start another.