When the user executes a command by using the full path, the shell uses that path to find the command. However, when users specify only a command name, the shell searches the directories for the command in the order specified by the PATH variable. If the command is found in one of the directories, the shell executes the command.
A default path is set by the system. However, most users modify it to add other command directories. Many user problems related to setting up the environment and accessing the correct version of a command or a tool can be traced to incorrectly defined paths.
Here are some guidelines for setting up efficient PATH variables:
If security is not a concern, put the current working directory (.) first in the path. However, including the current working directory in the path poses a security risk that you might want to avoid, especially for superuser.
Keep the search path as short as possible. The shell searches each directory in the path. If a command is not found, long searches can slow down system performance.
The search path is read from left to right, so you should put directories for commonly used commands at the beginning of the path.
Make sure that directories are not duplicated in the path.
Avoid searching large directories, if possible. Put large directories at the end of the path.
Put local directories before NFS mounted directories to lessen the chance of “hanging” when the NFS server does not respond. This strategy also reduces unnecessary network traffic.
This is an example of how to set a user's default path.
The following examples show how to set a user's default path to include the home directory and other NFS mounted directories. The current working directory is specified first in the path. In a C-shell user initialization file, you would add the following:
set path=(. /usr/bin $HOME/bin /net/glrr/files1/bin) |
In a Bourne-shell or Korn-shell user initialization file, you would add the following:
PATH=.:/usr/bin:/$HOME/bin:/net/glrr/files1/bin export PATH |