Linux - Add A Directory To PATH (Temporarily or Permanently
Linux - Add A Directory To PATH (Temporarily or Permanently
com/kb/linux-add-to-path
PATH is an environment variable that instructs a Linux system in which directories to search for executables. The P
ATH variable enables the user to run a command without specifying a path.
This article will explain how to add a directory to PATH temporarily or permanently as well as how to remove it
in Linux.
Prerequisites
• Access to the terminal.
• A text editor.
The output shows directories configured in PATH by default. The printenv command delivers the same output:
Furthermore, running which on a certain command shows where its executable is. For instance, execute which with w
hoami:
The output shows that the executable for whoami is located in the /usr/bin/ directory.
The command added Directory1 from the Home directory to PATH. Verify the result with:
The output shows that the directory was added to the variable. This configuration lasts during the current session only.
Linux: Add to PATH Permanently
Add a directory to PATH permanently by editing the .bashrc file located in the Home directory. Follow these steps:
1. Open the .bashrc file using a text editor. The example below uses Vim.
Editing the .bashrc file adds a directory for the current user only. To add the directory to the PATH for all users, edit the
.profile file:
The command only removes the string from the current session.
Method 4: Use a One-Liner
Another option is to use the combination of tr, grep and paste to remove a directory from PATH. For instance:
export PATH="$( echo $PATH| tr : '\n' |grep -v Directory1 | paste -s -d: )" Copy
Conclusion
After reading this guide, you now know how to add a directory to the PATH variable. Next, learn how to export Bash
variables in Linux.