The PATH variable controls where the shell searches for commands when you type them at the prompt (see Shell and Environment Variables, section 3.5, page
). When you type a non-builtin command to the shell, the shell searches for a program to execute. The programs are simply executable files somewhere in the file system; they are executable either because they are compiled programs or because they are scripts that may be executed. They also need to have their `execute permission' enabled (see Changing Permissions in section 2.2.17, page
).
Since we don't want to go through all the files on the file system, we have a path of directories where the program may be stored. This path is given to the shell as a colon-separated list of directories stored in the environment variable PATH. This means that any executables or other files within that directory can be accessed by typing only the filename without having to specify the entire directory.
For example, if the PATH variable has been set to /usr/local/java, then in order to access the java compiler, the user would only need to type:
% javac
However, if the PATH variable had NOT been set or did not include /usr/local/java, the user would need to specify the location by typing:
% /usr/local/java/javac
The PATH variable for a user is usually set in the user's .profile file. You can check your PATH at any time by typing:
% echo $PATH
It will look something like the following colon-separated list of directories.
/import/bizet/B/angies/Bin:/usr/local/bin:/usr/bin:/usr/etc:/usr/sbin:/usr/local/bin:/usr/local/X11/bin:/usr/local/etc:/usr/openwin/bin:/usr/openwin/demo:/sbin:/etc:/usr/local/X11R6/bin:/usr/local/share/bin:/bin
Caution: It is generally a bad idea to include `.' (the current directory) in your PATH. This makes you vulnerable to Trojan horses and other mistakes.
The which command is a command locator. It will find which version of the command in your PATH will be run. Some shells have a where command which will find all the versions of the command in your PATH. Within bash, the command `type -a' does the same thing. You can then choose to modify your PATH or just type in the full path of the version of command you want. See the manpage for your shell for advice on changing your PATH by typing:
% man