The tips provided in this article include two methods to add the persistent PATH for both specific and all users, being useful for every Linux distribution.
The first two sections of the content go straight to the point, describing the steps to add a path permanently. After which, you can find a short explanation on environment variables.
All instructions explained in this article contain screenshots, making it easy to understand and execute the examples.
How to Add a PATH Permanently to a Specific User in Linux
The first step before we start is to check our current PATH environment variable.
You can do it in a simple way by executing the echo command followed by a dollar sign ($) and the environment variable whose value you want to see which, in this case is the PATH, as shown in the following screenshot:
As you can see, there are 6 paths separated by colon. All paths must be separated by colon.
Another way to check all your environment variables including PATH is by running the env command as shown in the following example:
NOTE: While writing this tutorial, I edited my paths several times. That’s why you will see different values in the screenshots.
As you can see in the given figure, all environment variables are listed including the user paths.
To add a permanent path, one of the methods is to edit the hidden file .bashrc. You can print the hidden files by running the ls command followed by the –a (All) flag in the home directory.
To edit the .bashrc file, add a permanent path. Use the text editor of your preference. In my case, I used nano, as shown in the following:
In this first example, I added the fictitious path /home/linuxhint/something/default/bin where linuxhint is the user home.
The syntax is the following, where <PATH> must be replaced with the actual path that you want to add:
In this case, I added the following line:
Alternatively, you can use the echo command to append the line using the following syntax where ‘Line Content’ must be replaced with the full command. And File must be replaced with the file you are editing (.bashrc).
The practical example is the following, as stated previously, in case I want to add the path $HOME/something/default/bin or /home/linuxhint/something/default/bin (which are the same):
Update your environment variables by running the following command:
To print all environment variables including updated paths, you can use the env command.
To show only the PATH environment variable, run the command shown in the following figure:
Another way to add a path to the user environment variable is by editing the “.profile” file located in the home directory.
This time, let’s use the ls command followed by the –l flag to show all files including the hidden files.
As you can see, there is a file named “.profile”. Edit it using the text editor of your choice. In my case, I used nano.
Find the line similar to the one pointed by the white arrow in the following figure:
In this example, I added the /home/linuxhint/something2/default/bin path.
Below the found line, add a line as shown at the end of the following image, replacing the /home/linuxhint/something2/default/bin with the actual path that you want to add:
Update your environment variable by executing the command shown in the following image:
Check the updated path with the following command:
Or print all the environment variables including PATH using the env command:
You can find the instructions to add a permanent path for all users in the following discussion.
How to Globally Add a Path Permanently to All Users in Linux
This section shows how to add a global persistent path environment variable for all users.
This can be done by editing two files, /etc/profile and /etc/bash.bashrc.
In the first example, I will show you how to edit the /etc/profile file.
Use the text editor that you used to modify the /etc/profile.
At the end of the file, add the following line where /opt/something10/bin must be replaced with the path that you want to add.
Update the environment variable PATH using the source command as done in the previous section of this article.
Check if the path was properly added using the echo command as shown in the following image:
Another way to permanently add a path globally is by editing the /etc/bash.bashrc file using the text editor to open it.
In the following example, I added the new path /opt/something20/bin.
Append a similar line as the following, replacing the /opt/something20/bin with the actual path that you want to add.
Update the PATH environment variable using the source command. Then, check it by running the echo command as shown in the following example:
As you can see, both methods worked successfully and the paths were added.
User vs System Wide Environment Variables
The difference between the user specific and the global environment variables is the following:
- User Environment Variables: User environment variables like PATH are defined in the user home configuration files. They are loaded from the home directory when the user starts a session.
- System Wide Variables: This type of variables don’t belong to a specific user, but to the whole system affecting all users.
This tutorial deeply explained how to add a PATH variable. To edit the user environment variables, you need to edit the .bashrc or .profile files located in the home directory. The variables exported to this file will load every time the user starts a session.
The system variables are stored in the /etc/bash.bashrc or /etc/profile files.
As you can see, adding the PATH variables both temporarily and permanently is pretty easy and can be done by any Linux user independently of the knowledge level. Every Linux user must understand the PATH variable function and how to manage it. Other variables are also deeply explained at Linux Hint. The content previously explained is valid for almost every Linux distribution.
Thank you for reading this tutorial showing how to permanently add the PATH variable. Keep following us for more professional content.