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
- Add Directory to a Linux PATH Temporarily
- Add Directory to a Linux PATH Permanently
- Remove the Directory from the Linux PATH
Execute the following command to display all the directories present in the system’s PATH:
You can also use printenv command to print the paths list:
As you can see, multiple paths are displayed and separated by a colon ( : ).
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:
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):
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:
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:
Type the mentioned command in the .bashrc file:
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:
And then run the echo command to see the result:
The directory is permanently added to the Linux PATH and it will keep in the .bashrc file even when the session is logged out.
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:
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:
3. You can also remove the directory from a Linux APTH through a string replacement. For this, the following command syntax would be used:
Suppose, to remove the /LinuxDirectory from a PATH, run the command mentioned below:
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:
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.