PATH is an environmental variable present in almost all major operating systems that define a set of directories where executable programs are located. Programs and scripts located in the directories specified in $PATH can be executed directly without specifying their path.
Executable programs like ls, find, and the file is located at different directories in an Operating system. When a person inputs a command, the system looks for an executable program (with the same name) in the directories specified in $PATH.
In this post, you will learn in detail how to add directories to $PATH in CentOS 8.
Check Directories in $PATH
First, let’s check which directories are already present in your $PATH. To do that, run the echo or printenv command:
The output lists all the directories specified in $PATH, separated by colons. You can easily add or remove directories by editing the user’s shell profile.
Variable names are case-sensitive. If you have two programs with the same name, then the shell will run the program that is in the directory, which comes first.
Sometimes you may need to add other directories to your $PATH variable. For example, you may have a separate directory for your own scripts that you need to run repeatedly. Specifying their absolute path in the terminal each time is time-consuming. So you can just add their directory to $PATH.
Add a directory to the $PATH using the export command
To add a directory to the $PATH, we will use the export command.
Remember to replace path/dir with the actual path of the directory.
The modified variable will be exported to the shell child process environments by the export command. Then you can run the executable file located in the directory without specifying its entire path.
This modification is only temporary and will only work in the current session. Once you end the current shell session, the $PATH variable will reset.
To make this modification permanent, the $PATH variable must be specified in the shell configuration files.
Add a directory permanently to $PATH
If you want the directory to be added to all system user’s $PATH, use Global shell specification files such as /etc/environment. For a single user, you have to use Per-user shell-specific configuration files. In this example, we will use the ~/.bashrc file as we are using bash.
We will use the ~/.bashrc file in this example. Go ahead and use the default text editor to open the file:
Add the following line at the end of the file:
Save and exit the file. Now using the source command, load the new modified $PATH variable into the current session.
Now again, use the echo command to check directories that are present in $PATH. This will verify whether the directory was successfully added.
It is an excellent practice to make a dedicated folder for all of your executable programs and scripts. This will make it a lot easier for you to run the scripts/programs stored on nonstandard locations as you won’t need to type their full path.
This article learned how to permanently and temporarily add new directories to the $PATH variable. The instructions given above will work for most Linux distributions like CentOS, Ubuntu, and RHEL.