This article will explain how to add a directory to
PATH temporarily or permanently as well as how to remove it in Linux.
- Access to the terminal.
- A text editor.
What Is Linux PATH?
When a user invokes a command in the terminal, the system executes a program. Therefore, Linux has to be able to locate the correct executable.
PATH specifies program directories and instructs the system where to search for a program to run.
How to View the Directories in PATH
To print all the configured directories in the system’s
PATH variable, run the echo command:
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
How Do I Add a Directory to PATH in Linux?
Specific directories are added to
PATH by default. Users can add other directories to
PATH either temporarily or permanently.
Linux: Add to PATH Temporarily
Temporarily adding a directory to
PATH affects the current terminal session only. Once users close the terminal, the directory is removed.
To temporarily add a directory to
PATH, use the export
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.
2. Go to the end of the file.
3. Paste the export syntax at the end of the file.
4. Save and exit.
5. Execute the script or reboot the system to make the changes live.
6. To verify the changes, run
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:
Remove Directory from PATH in Linux
There is no single command to remove a directory from
PATH. Still, several options enable the process.
Method 1: Exit the Terminal
Removing a directory from
PATH is simple when it’s added temporarily. Adding the directory in the terminal works for the current session only. Once the current session ends, the directory is removed from
To delete a temporary directory from
PATH, exit the terminal or reboot the system.
Method 2: Edit Configuration Files
If the directory export string was added to the .bashrc or .profile file, remove it using the same method. Open the file in a text editor, navigate to the end of the file, and remove the directory.
Method 3: Apply the String Replacement Concept
To remove a directory from
PATH, use string replacement:
The command only removes the string from the current session.
Method 4: Use a One-Liner
export PATH="$( echo $PATH| tr : 'n' |grep -v Directory1 | paste -s -d: )"
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.
Đăng ký liền tay Nhận Ngay Bài Mới
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !