The PATH variable in Linux stores the path to the directories where it should look for executables when you run a command.
abhishek@its-foss:~$ 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 a colon (:).
If you want to run some executables as commands from anywhere in the system, you should add their location in the PATH variable.
This is common while setting up a development environment. For example, imagine you downloaded and installed Java and Maven. To make your programs work properly, you’ll need to specify the location of the binaries of Maven and Java in the PATH.
This quick tutorial is about setting up PATH in Linux. Apart from the steps, I’ll also mention things you should be careful about while dealing with PATH.
Adding a directory to PATH in Linux
The process to add a new directory to the PATH variable in Linux is essentially this:
your_directory is the absolute path to the concerned directory.
Let’s say you download and extract Maven to the home directory and you want to add its bin directory to the PATH. Let’s assume that the absolute path of this bin directory is /home/abhishek/maven/apache-maven-3.8.0/bin.
Here’s what you should be doing:
Things to pay attention here:
- The $ before a 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 on the left side of =
- There must not be any spaces before and after =
- Don’t forget to include the : after $PATH because the directories in the PATH are separated by a colon.
- There must not be a space before and after the colon (:).
Once you have set the PATH with the new value, please check that the PATH has been correctly updated.
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 may want to run the command or script for which you modified the PATH. This will tell you for sure if the PATH is correctly set now.
Making the changes to PATH permanent
You added the desired directory to the PATH variable, but the change is temporary. If you exit the terminal, exit the session or log out from the system, the PATH will revert, and the changes will be lost.
If you want to make the changes to the PATH variable permanent for yourself, you can add it to the .bashrc file in your home directory, assuming you are 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 for everyone on the Linux system, you can add the export to the /etc/profile file. This is suitable when you are a sysadmin and have a configured system with a custom path.
💡See the path in a simplified view
Reading the PATH variable values and figuring out the directories could be complicated for some people. You can use the tr command to format its output to make it more easily readable.
echo $PATH | tr ':' '\n'
Here's an example:
Bonus tip: The directories take precedence in PATH
There are several directories in the PATH variable. When you run an executable file/command, your system looks into 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 searched first in /usr/local/sbin. If the executable is found, the search ends and the executable is executed.
This is why you’ll find some examples where the additional directory is added before everything else in PATH:
If you think that your additional directory should be searched before everything else you should add it before the $PATH; otherwise, add it after $PATH.
Was it clear enough?
I highly suggest reading up on the concept of absolute and relative paths in Linux. It will give you a better understanding of the topic.
I have tried to explain things with the necessary details but not going too deep into details. Does it make the topic clear, or are you more confused than before? If you still have doubts, please let me know in the comments.