How to add a directory to the PATH variable in Linux

The PATH variable in Linux stores the path to directories containing command files and is called by the user on the terminal. For example, when you type python in the terminal, the terminal will look for directories containing a file named python to execute. Instead of you having to go to that directory and then call python, you can now call python at any path.

How to add a directory to the PATH variable in Linux

To see what paths the PATH variable is currently storing, type the following command: echo $PATH

Result: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

As you can see, PATH includes directories (like /usr/local/sbin, /usr/bin and many more) separated by a colon (:).

If you want to execute script files from anywhere on the system, you should add their location in the PATH variable.

This is very common when setting up a development environment. For example, imagine you have downloaded and installed Java and Maven. In order for your programs to work properly, you will need to specify the location of Maven and Java in the PATH.

So, this article will show you how to set PATH in Linux. In addition, I will also cover the things you should be careful about when handling PATH.

Add directory to PATH in Linux

The process of adding a new directory to the PATH variable in Linux is basically the following:

export PATH=$PATH:your_directory

Where your_directory is the absolute path to the directory you want.

Let’s say, you have downloaded and unzipped Maven to the home directory. You then want to add the Maven bin directory to your PATH. Assume the absolute path of this bin directory is /home/abhishek/maven/apache-maven-3.8.0/bin.

Here’s what you should do:

export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin

Explain:

  • Adding $ before the variable name means you are referring to its value. PATH is the variable name, $PATH is the value of the variable PATH
  • You should not use $ with PATH to the left of the equal sign “=”
  • There cannot be any spaces before and after the equal sign “=”
  • Don’t forget to add a “:” after $PATH because directories in PATH are separated by colons
  • There must be no spaces before and after the colon (:)

Once you’ve added the new path to your PATH, double check the PATH is correct.

abhishek@its-foss:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/abhishek/maven/apache-maven-3.8.0/bin

You can run the new script file added to the PATH. This will help you to check if the PATH is working or not.

Make permanent changes to PATH

You’ve added the directory to the PATH variable but that’s only a temporary change. If you close the terminal, or log out of the system, the PATH will be back to the way it was and the changes you made will be gone.

If you want to make permanent changes to the PATH variable yourself, you can add these changes to the .bashrc file in your home directory, assuming you’re using the Bash shell.

Use text editors like Nano or Vim for this.

nano ~/.bashrc

Make permanent changes to PATH

If you want the modified PATH variable to be applied to users on a Linux system, you can add these changes to the /etc/profile. This will work when you are a sysadmin and have a system configured with a custom path.

Preferred directories in PATH

There are several directories in the PATH variable. When you run an executable, your system searches the directories in the order in which they appear in the PATH variable.

If /usr/local/sbin precedes /usr/bin, the executable is searched first in /usr/local/sbin. If the executable is found, the search ends and the file is executed.

This is why you will see some commands where extra directories are added before anything in the PATH variable:

export PATH=your_directory:$PATH

If you think your extra directory should be searched before other directories, you should add it before $PATH, otherwise add it after $PATH.

So that’s it then. Also, if you forgot your Linux password on WSL, you can read this article.

Leave a Reply