Executable search path

The executable for the ls command is stored in the directory /bin. When you enter ls at the command line, you are instructing the shell to run the executable ls. How does the shell know to find ls in the directory /bin? The executable search path, or just ``the path,'' is an ordered list of directories that will be searched by the shell for executables. The directories are searched in the order in which they are listed in your path.


You can view the current setting of your path by running

echo ${PATH}
in bash or
echo ${path}
in tcsh. If you've installed some new software whose executable isn't in your current path, then you'll need to change your path.

Here's an example of changing a path. Suppose you want to search, in order,

${HOME}/bin
/usr/local/matlab/bin
/usr/local/bin
/usr/games/zombie-zapper/bin
followed by whatever directories the system would ordinarily search. In tcsh you would run
set path = (${HOME}/bin /usr/local/matlab/bin /usr/local/bin /usr/games/zombie-zapper/bin $path)
and in bash you'd run
export PATH=${HOME}/bin:/usr/local/matlab/bin:/usr/local/bin:/usr/games/zombie-zapper/bin:$PATH

Note that the previous path, $path, is appended to the end of the list. This is important. When you start the shell it sets a default path that lets you find commonly-used commands. By including the current path in your new path, you preserve that information. By putting the current path at the end, you ensure that the standard directories are searched last. That is essential, because sometimes you will want to use a custom version of a common command; if the custom version is located in a directory appearing earlier in the path, it will be used instead of the standard version.

You won't want to do this manually every time you start a new shell. To automate the procedure, put the command to set the path in your .tcshrc file (if you run tcsh) or .bashrc file (if you run bash). It will take effect when you start a new shell. To have it take effect immediately, run

source .tcshrc
or
source .bashrc
as suits your choice of shell.

IMPORTANT SECURITY ISSUE: By default, your search path does not include the current directory. This is a minor inconvenience, because it means to run myProgram.exe in the current directory, you need to invoke

./myProgram.exe
instead of the simpler
myProgram.exe
To avoid this inconvenience it's possible to add the current directory to the path, but do not do so. NEVER ADD THE CURRENT DIRECTORY TO YOUR PATH. Here's why: if you visit a website that's been hacked, an attacker can place in the current directory an altered version of a frequently-used executable such as ls. You probably won't think to look for it, and so won't notice it's there. If the current directory is early in your path, the next time you try to run ls the altered version will be run. The altered version will then do whatever damage was intended by the attacker, mimc the real ls, then remove itself. Leaving the current directory out of your path closes this security hole.
Kevin Long
Last modified: Wed Sep 8 23:45:06 CDT 2010