Kiến Thức Linux

Linux: Add a Directory to PATH


PATH is an environment variable that instructs a Linux system in which directories to search for executables. The PATH 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.

Linux: add a directory to PATH


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:

echo $PATH
echo PATH terminal output

The output shows directories configured in PATH by default. The printenv command delivers the same output:

printenv PATH
printenv PATH terminal output

Furthermore, running which on a certain command shows where its executable is. For instance, execute which with whoami:

which whoami
which whoami terminal output

The output shows that the executable for whoami is located in the /usr/bin/ directory.

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 PATH command:

export PATH="/Directory1:$PATH"
export PATH terminal output

The command added Directory1 from the Home directory to PATH. Verify the result with:

echo $PATH
echo PATH Directory1 added terminal output

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.

Opening .bashrc in Vim.

2. Go to the end of the file.

3. Paste the export syntax at the end of the file.

export PATH="/Directory1:$PATH"
Add directory to .bashrc file in Vim

4. Save and exit.

5. Execute the script or reboot the system to make the changes live.

6. To verify the changes, run echo:

echo PATH permanently added Directory1 terminal output

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:

Add directory to profile file in Vim

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 PATH automatically.

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:

export PATH=${PATH/'/Directory1'/}
String replacement terminal output

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: )"
tr grep -v paste terminal output


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

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !