Add a directory to the current path
|Debian (Etch, Lenny, Squeeze)|
|Ubuntu (Hardy, Intrepid, Jaunty, Karmic, Lucid, Maverick, Natty, Oneiric, Precise)|
To append or prepend a directory to the current search path
PATH variable contains a colon-separated list of directories that are searched by the shell when looking for a command. For example, suppose that the shell is attempting to execute the
sync command. Given a
it would look first for an executable named
/usr/local/bin/sync. If that were not found it would try
/usr/bin/sync, and then finally,
Each process has its own path. The initial value is copied from the relevant parent process, but from then on it can be altered independently. An appropriate default path should normally have been set during login. It is possible for different users to have different default paths.
When new software is installed it is usally best for any new commands to be installed in or softlinked from a directory that is already part of the default path. However it may be appropriate to use a different location if you do not wish to make the commands visible to all users, or if you do not have write access to a directory on the default path.
Suppose you have installed a copy of the Java JDK in the directory
/usr/local/jdk1.7.0. This contains a subdirectory named
bin containing a number of utility programs that form part of the JDK. In order to facilitate the use of these programs you wish to add the directory
/usr/local/jdk1.7.0/bin to your path.
To append the directory (so that it is searched last), construct a new value for the
PATH variable which incorporates the existing path as a prefix:
Alternatively, to prepend the directory (so that it is searched first) the existing path should be included as a suffix:
Be warned that this will only affect the path seen by the shell process within which this command is executed, and by children of that process. Changes made in a child process are not passed back to the parent (so you cannot, for example, write a shell script to change the path of the caller).
The method above assumes that a non-null
PATH variable already exists as part of the environment. It will misbehave in two ways if this is not the case:
- The newly-created
PATHvariable will not be marked for export, and thus not become part of the environment of any subprocesses.
- There will be a spurious colon at the start or end of the path, which will cause the current working directory to be included in the path. (This is not how you are supposed to include the current directory, which should be represented by a full stop, but POSIX-compatible shells accept a zero-length prefix as an alternative representation. See below for a discussion of the security risks that result from including the current directory.)
These issues can be avoided by making the expansion conditional on whether the
PATH variable exists and is non-null, and by using the
export command to ensure that it is always marked for export. To append:
and to prepend:
In practice it is unusual for the path to be completely empty, so the possibility is often ignored.
It was once common practice for the current working directory to be included as part of the default path. Unfortunately this makes it possible to lay an ambush for other users by placing executables in directories that they are likely to visit. The risk is greatest if the current directory appears at the start of the path, because then it is possible to override existing commands. Even if it is placed at the end then there is still a risk from typing errors (made more likely by the autocompletion facility provided by most modern shells).
Similar considerations apply to other directories if you cannot fully trust the content, but it is inclusion of the current directory that poses the greatest risk because that potentially allows the trap to be sprung anywhere. The potential harm is magnified greatly when the shell in question has administrative privileges.