Rate this page

Add a directory to the current path

Tested on

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


The 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 PATH of:


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, /bin/sync.

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).


Handling an empty or non-existant path

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:

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:

export PATH="${PATH:+$PATH:}/usr/local/jdk1.7.0/bin"

and to prepend:

export PATH="/usr/local/jdk1.7.0/bin${PATH:+:$PATH}"

In practice it is unusual for the path to be completely empty, so the possibility is often ignored.

Security considerations

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.

See also

Tags: environment | shell