Linux Commands

Add Directory to a Path in Linux

Before getting started with the definition of PATH in Linux distributions, we need to understand Environment Variables first. In simple, Environment Variables are the dynamic values used by a shell or its child process. It consists of a variable along with the associated value that defines the behavior of the environment.

One of the environment variables in Linux is $PATH, which helps to search the Linux directories to make them executable. It is the crucial part of any operating system containing a list of locations that can be called anytime using a command in the shell terminal.

Display Paths List

Execute the following command to display all the directories present in the system’s PATH:

echo $PATH

You can also use printenv command to print the paths list:

printenv PATH

As you can see, multiple paths are displayed and separated by a colon ( : ).

Add Directory to a Linux PATH Temporarily

We can use the export command to temporarily add the directory in a Path. Using the temporary session will limit to the current working shell only, once the shell terminal is closed, the added directory will also be removed.

Suppose we have a directory “LinuxDirectory” in the Home directory; to add this to the Path, we need to run the following command:

export PATH="$Home/LinuxDirectory:$PATH"

In the above command, the first PATH without dollar ($) sign is a variable in which we are adding path values. The export command will export the added PATH value. Whereas the $PATH is also the value of the variable PATH.

Run the echo command to verify the output (if directory has been added to the Path):

echo $PATH

Remember: As mentioned above, this directory will be removed from the Path whenever the shell terminal session is ended. To add permanently, follow the steps mentioned below:

Add Directory to a Linux PATH Permanently

To make changes permanently (adding a directory to a PATH), we need to edit the .bashrc file which is present in the Home directory. You can open this file using any editor like Nano or Vim:

vim .bashrc

Type the mentioned command in the .bashrc file:

export PATH="$Home/LinuxDirectory:$PATH"

Save the bash file by typing :w in the command mode and then :qa to exit it:

Now, back to the terminal and run the source command to make the changes:

source ~/.bashrc

And then run the echo command to see the result:

echo $PATH

The directory is permanently added to the Linux PATH and it will keep in the .bashrc file even when the session is logged out.

Remove the Directory from the Linux PATH

We have numerous ways to delete an added directory from the Linux PATH.

1. If you have added a directory temporarily, then just close the terminal session or reboot the system, the directory will be removed.

2. If you have added the directory permanently i-e, using the configuration file, then open the .bashrc file using any editor and remove the export command from there:

vim .bashrc

Save the file and close the terminal to logout from the current session; open the terminal again and run the echo command to find if the directory has been deleted or not:

echo $PATH

3. You can also remove the directory from a Linux APTH through a string replacement. For this, the following command syntax would be used:

export PATH=${PATH/'/directory_name'/}

Suppose, to remove the /LinuxDirectory from a PATH, run the command mentioned below:

export PATH=${PATH/'/LinuxDirectory'/}

4. Another way is to combine multiple commands i-e, tr, grep, and paste to delete the directory from the Linux Path; copy the given command and paste it into a terminal:

export PATH="$( echo $PATH| tr : '\n' |grep -v LinuxDirectory | paste -s -d: )"

Conclusion

The PATH is the environment variable containing lists of variables/directories and making them executable when called using a command in a shell terminal. In Linux, we can add a directory to a path temporarily and also permanently. This tutorial has mentioned a step-by-step procedure to add a directory in a Linux PATH. We have also discussed various processes to remove it using the command-line tools.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.