This article discusses an important Linux shell environment variable called PATH and how you can add a directory to this variable.
In general, most programs you’ll ever run on your Linux system will work flawlessly, and your shell will not present any errors at the time of their execution. However, there may be times when you might want to quickly run custom scripts or programs from non-standard directories on your Linux system.
In such cases, you need to add their source directory to your shell’s PATH variable so that it knows where to find them.
What Is PATH Environment Variable Used For?
The environment variables control the behavior of the shell. For example, if you ever use the command line on Linux, the system relies on the PATH variable to find the location of the commands you are entering.
PATH is a built-in environmental variable in Linux that tells the shell which directories to search for executable files in response to commands issued by a user or application.
It holds the colon-separated list of directories used to find commands that you enter.
How to Add a Directory to PATH in Linux
To see what’s in your PATH right now, type this into a terminal:
As you can see, the directories in this variable are separated by colon (
:). At present, the shell searches the following directories for the binary executable files:
The order of the search PATH is also important. So, if you have two executable files sharing the same name located in two different directories, the shell will run the file in the directory that comes first in the PATH.
Add a Directory to PATH Temporarily
Now assume that you have created a custom shell script located in the
/srv/scripts directory, and you want to add this directory to your PATH variable. Here’s what you should be doing:
To make things more transparent, let’s break down this syntax. First, here is what each parameter means:
exportcommand will export the modified
PATHvariable to the shell child process environments.
$before a variable name means you are referring to its value.
:/srv/scriptssection specifies that the content after the
:symbol should be appended to the values contained in the current PATH variable.
Let’s see what the PATH looks like now:
As you can see in the above output, the
/srv/scripts directory is added to the end of the PATH variable. Now, files you have stored in the
/srv/scripts directory can be executed anywhere without specifying their full path.
In addition, if you think that your directory should be searched before everything else, you could add it before the
It’s important to note that the PATH will revert if you exit the terminal or log out from the system. The changes will be lost because this method of setting PATH remembers the changes only temporarily during the current terminal session.
To make it permanent, check out the section below.
Add a Directory to PATH Permanently
To make the change permanent, you need to define the PATH variable in the shell configuration files.
The default system-wide PATH value is specified in the
/etc/profile file. The best place to add a directory to the path of a single user is to modify that user’s
Open the file with your text editor, scroll to the bottom of the file, and add the following line at the end of it:
Finally, save the file and load the new PATH into the current shell session using the
To confirm that the directory was successfully added, type the following
echo command to check the path:
As you can see, adding new directories to your user or global PATH variable in Linux is pretty simple. However, in this article, we have learned that there are two ways of doing it: temporarily and permanently.
If you still have doubts, please let me know in the comments.