Linux: Add a Directory to PATH

Introduction

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

Prerequisites

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

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.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use the hostname Command in Linux
October 8, 2020

The Linux hostname command lets you view your computers domain, hostname, and IP...
Read more
Bash printf - How to Print a Variable in Bash
February 24, 2022

The printf command produces formatted text outputs in the terminal. This tutorial shows how to use the printf command to print and format a variable output in Linux...
Read more
How to Use the Linux cd Command to Change Directory
December 22, 2023

The cd command in Linux allows you to change the current working directory using the terminal window. Learn how to use the cd command in this tutorial...
Read more
How to Remove a Directory in Linux
January 18, 2024

Linux offers different ways to remove directories without using the GUI. In this tutorial, learn how to remove directories in the terminal window or command line using Linux commands...
Read more