As mentioned previously, the environment variable PATH stores a list of directory paths that the shell will search, in order, when you issue a command. $PATH refers to the value assigned to this variable. The paths in $PATH may be modified by the OS, a system administrator, environment management tools, or directly by the user. You can view the current list of directories with echo $PATH.

If a command you issue is not found in any of the paths in $PATH, then the shell will not be able to execute it, unless it is prefaced by the full path to the command. You can change the directories in the list by assigning a new value to PATH. Since PATH is an environment variable, you typically do this through the export command, which ensures that the new $PATH value is passed down to subshells of the current shell.

Add directory to path

To add directories to your path, you can use:

$ export PATH=$PATH:/path/to/new/command

Again, the $PATH on the right-hand side of the = means "the current value of the PATH variable." The : joins directories in the path variable together. Inserting $PATH: before the directory you want to add has the effect of appending the new directory to the list of existing directory paths. If you want your additional directory to be the first one searched for commands, put it immediately after the = and follow it with :$PATH. Note that no spaces should appear on either side of the = sign.

Redefine path

If you wanted to completely replace the list with a different path:

$ export PATH=/path/to/replacement/directory
Warning: This command completely replaces the previous path.

Please keep in mind that using export PATH=/path/to/replacement/directory will erase the previous list of directories in your path.

Try env to list all environment variables. Examples such as SHELL, HOME, and PATH are built-in environment variables. Any variable can be declared to the shell, just locally, by typing

$ MYVAR=something

To make a shell variable into an environment variable, you need to export it. This places the variable into the environment of the current shell, plus any subshells that are started from that shell. You can export an existing shell variable, or you can create and export a shell variable in a single line:


$ export MYVAR
$ export MYVAR2=something_else

Conventionally, shell variables are declared in all capital letters. The value of the variable can then be used in bash commands or scripts by inserting $MYVAR. Note again the use of a preceding $ when using the variable and the absence of the $ prefix when setting the variable value. If you want to insert the variable inside a string, it is safest to do this by using the form ${MYVAR} and placing the beginning and ending portions of the string on either side. This cleanly distinguishes the name of the variable from any neighboring characters in the string. For example:

$ DATAPATH=/home/jolo/Project
$ ls $DATAPATH
$ cp newdatafile.txt ${DATAPATH}/todaysdatafile.txt
$ ls $DATAPATH
todaysdatafile.txt
 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement