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:
echo $PATH
Code language: PHP (php)
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:
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin
/home/linuxiac/.dotnet/tools
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:
export PATH=$PATH:/srv/scripts
Code language: JavaScript (javascript)
To make things more transparent, letโs break down this syntax. First, here is what each parameter means:
- The
export
command will export the modifiedPATH
variable to the shell child process environments. - The
$
before a variable name means you are referring to its value. - The
:/srv/scripts
section 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:
echo $PATH
Code language: PHP (php)
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 $PATH
.
export PATH=/srv/scripts:$PATH
Code language: JavaScript (javascript)
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 .bashrc
file.
Open the file with your text editor, scroll to the bottom of the file, and add the following line at the end of it:
vim ~/.bashrc
export PATH=$PATH:/srv/scripts
Code language: JavaScript (javascript)
Finally, save the file and load the new PATH into the current shell session using the source
command:
source ~/.bashrc
To confirm that the directory was successfully added, type the following echo
command to check the path:
echo $PATH
Code language: PHP (php)
Conclusion
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.