How to Add a Directory to PATH in Linux [Quick Tip]
The PATH variable in Linux stores the path to directories where it should look for executables when you run a command.
[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
As you can see, the PATH consists of several directories (like /usr/local/sbin, /usr/bin and more) separated by colons (:).
If you want to run certain executables as commands from anywhere in the system, you need to add their location in the PATH variable.
This is common when setting up a development environment. For example, imagine you have downloaded and installed Java and Maven. For your programs to work correctly, you will need to specify the location of the Maven and Java binaries in the PATH.
This quick tutorial is about setting up PATH on Linux. Besides the steps, I will also mention the things you need to pay attention to when dealing with PATH.
Add directory to PATH in Linux
The process for adding a new directory to the PATH variable in Linux is basically as follows:
your_directory is the absolute path to the relevant directory.
Suppose you download and extract Maven to the home directory and want to add its bin directory to the PATH. Suppose the absolute path of this bin directory is /home/abhishek/maven/apache-maven-3.8.0/bin.
Here’s what you should do:
Things to be careful here:
- The $ in front of a variable name means you are referring to its value. PATH is the variable name, $PATH is the value of the PATH variable.
- You should not use $ with PATH to the left of =
- There should be no spaces before and after =
- Don’t forget to include the : after $PATH because the directories in the PATH are separated by colons.
- There should be no space before and after the colon (:).
Once you have set the PATH with the new value, please verify that the PATH has been updated correctly.
[email protected]:~$ 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 command or script for which you changed the PATH. This will tell you for sure if the PATH is set correctly now.
Make changes to PATH permanent
You added the desired directory to the PATH variable, but the change is temporary. If you exit the terminal, log out, or log out of the system, the PATH will be reset and the changes will be lost.
If you want changes to the PATH variable to be permanent, you can add it to the .bashrc file in your home directory, assuming you’re using the Bash shell.
You can use a text editor like Nano or Vim for this task.
If you want the modified PATH variable to be available to everyone on the Linux system, you can add the export to the /etc/profile file. This is fine when you are a system administrator and have a system configured with a custom path.
Bonus tip: directories take priority in PATH
There are several directories in the PATH variable. When you run an executable file/command, your system examines the directories in the same order as they are mentioned in the PATH variable.
If /usr/local/sbin comes before /usr/bin, the executable is first searched in /usr/local/sbin. If the executable is found, the search ends and the executable is executed.
That’s why you’ll find a few examples where the extra directory is added before everything else in PATH:
If you think your extra directory should be searched before everything else, you should add it before the $PATH; otherwise, add it after $PATH.
Was that clear enough?
I tried to explain things with the necessary details but without going into too much detail. Does it make the subject clear or are you more confused than before? If you still have any doubts, let me know in the comments.