0% found this document useful (0 votes)
7 views1 page

Linux - Add A Directory To PATH (Temporarily or Permanently

The document provides a comprehensive guide on how to add a directory to the PATH environment variable in Linux, both temporarily and permanently. It explains the significance of PATH, how to view its current directories, and methods for adding or removing directories. Additionally, it includes step-by-step instructions for editing configuration files and using command-line techniques for managing PATH entries.

Uploaded by

Zac Ing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Linux - Add A Directory To PATH (Temporarily or Permanently

The document provides a comprehensive guide on how to add a directory to the PATH environment variable in Linux, both temporarily and permanently. It explains the significance of PATH, how to view its current directories, and methods for adding or removing directories. Additionally, it includes step-by-step instructions for editing configuration files and using command-line techniques for managing PATH entries.

Uploaded by

Zac Ing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

https://phoenixnap.

com/kb/linux-add-to-path

Home » KB » SysAdmin » Linux: Add a Directory to PATH


Linux: Add a Directory to PATH
By Sara Zivanov Published: September 22, 2022
Topics: Commands, Environment Variable, Linux

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.

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 Copy

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

printenv PATH Copy

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

which whoami Copy

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" Copy

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

echo $PATH Copy

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.

export PATH="/Directory1:$PATH" Copy

4. Save and exit.


5. Execute the script or reboot the system to make the changes live.
6. To verify the changes, run echo:

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 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'/} Copy

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.

You might also like