Are you curious about how you can view or update the Linux PATH environment? Every time you install a program in Linux, the program gets stored in a directory. There are numerous directories containing the installed programs. When you want to run an installed program, your system will retrieve it from these directories using an environment variable which is the PATH. So, what is a Linux PATH and how can you view and update it? Stick around to find out!
Understanding the Linux PATH Environment
The PATH in the Linux environment is best described as a resource that tells the system where to find installed programs when you want to run them. The PATH contains a list fo directories separated by a colon. When you want to run a program, Linux will search for the program inside the directories following the order they appear in the PATH.
Once the executable file is located, only then will the program open. The idea behind these environment variable is that it allows you to run an installed program from any location without typing its full path.
For instance, if you want to use the cat command to open a file. You would run the cat command as follows.
Alternatively, you could run the cat command by typing its absolute file path which would be from a directory such as the one in the below command.
Both options give the same output.
How to View and Update the Linux PATH Environment
We’ve already discussed what the Linux PATH environment means and seen how it works when running commands. In this section, we will see how you can update the PATH environment. We’ve split into two.
Section 1: Viewing the Linux PATH Environment
First, let’s understand how you can view your system’s PATH variable. Run the below command.
You will get an output showing the list of directories separated by a colon. This list follows a priority sequence. Those listed first will be checked first until we reach the last directory.
Whenever you run a command to open a program, the system will search for the program from left to right. Suppose you want to add a directory to this PATH, you can add it as the first or last element in the list.
Section 2: Updating the Linux PATH Environment
There are some instances where you might consider updating the Linux PATH environment variable. For instance, if you want to use a different directory for your program, you can add it.
There are two ways of adding a directory to the PATH environment variable. You can add it at the first or last position. To add it as the first element on the list, add it at the beginning of the PATH keyword in your command.
Here’s an example.
When we view the PATH environment variable, we can see that our added directory is first on the list of directories. Therefore, when calling a program, this directory will be checked first.
Suppose you prefer adding it as the last element. Add the directory after the PATH like in the example below.
When we view the PATH, we notice that our directory is last on the list, meaning it will be checked last.
You can also add multiple directories at once. However, ensure you separate them using a colon. Here’s an example.
How to Make Permanent Updates of the Linux PATH Environment Variable
All the changes we’ve made to our Linux PATH environment are temporarily. They will disappear after a reboot. However, it’s possible to make them permanent.
To do so, we mist add the directory in the ~/.bashrc. Open the shell configuration file using a text editor. We’ve used the nano text editor for our case
Scroll to the bottom of the file and PATH environment variable.
Save the file and exit. The last step is to refresh the shell configuration file by running the command below.
That’s it. Even when you reboot your system, your changes to the shell configuration file will persist and your PATH environment variable will contain the added directory.
Conclusion
The Linux PATH environment stores a list of directories where the installed programs are located. This post has discussed what the Linux PATH environment variable is. Moreover, we’ve discussed how you can view and update the PATH environment variable.