How to Add a Directory to PATH in Linux [With Examples]

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:

echo $PATHCode language: PHP (php)
View the current PATH variable values

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/scriptsCode 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 modified PATH 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 $PATHCode language: PHP (php)
Adding directory to the PATH variable in Linux

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:$PATHCode 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/scriptsCode language: JavaScript (javascript)
Adding directory to the PATH variable permanently in Linux

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 $PATHCode language: PHP (php)
Adding directory to the PATH variable in Linux

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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%