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:

Output

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-11.png" data-lazy- height="79" src="data:image/svg xml,” width=”726″>

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.

$ export PATH=$path/dir:$PATH

Remember to replace path/dir with the actual path of the directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-11.png" data-lazy- height="42" src="data:image/svg xml,” width=”637″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-11.png" data-lazy- height="79" src="data:image/svg xml,” width=”726″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image5-11.png" data-lazy- height="539" src="data:image/svg xml,” width=”763″>

Add the following line at the end of the file:

export PATH=$path/dir:$PATH

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image4-11.png" data-lazy- height="530" src="data:image/svg xml,” width=”758″>

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.