- How to Add a Directory to PATH in Linux
- Adding a directory to PATH in Linux
- Making the changes to PATH permanent
- 💡See the path in a simplified view
- Bonus tip: The directories take precedence in PATH
- Was it clear enough?
- Linux: Add a Directory to PATH
- What Is Linux PATH?
- How to View the Directories in PATH
- How Do I Add a Directory to PATH in Linux?
- Linux: Add to PATH Temporarily
- Linux: Add to PATH Permanently
- Remove Directory from PATH in Linux
- Method 1: Exit the Terminal
- Method 2: Edit Configuration Files
- Method 3: Apply the String Replacement Concept
- Method 4: Use a One-Liner
- How to Add a Directory to PATH in Linux
- What is $PATH in Linux #
- Adding a Directory to your $PATH #
- Conclusion #
How to Add a Directory to PATH in Linux
Learn all the essential steps about adding a directory to the PATH in Linux and making those changes permanently.
The PATH variable in Linux stores the path to the directories where it should look for executables when you run a command.
[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
As you can see, the PATH consists of several directories (like /usr/local/sbin, /usr/bin and more) separated by a colon (:). If you want to run some executables as commands from anywhere in the system, you should add their location in the PATH variable. This is common while setting up a development environment. For example, imagine you downloaded and installed Java and Maven. To make your programs work properly, you’ll need to specify the location of the binaries of Maven and Java in the PATH. This quick tutorial is about setting up PATH in Linux. Apart from the steps, I’ll also mention things you should be careful about while dealing with PATH.
Adding a directory to PATH in Linux
export PATH=$PATH:your_directory
Where your_directory is the absolute path to the concerned directory. Let’s say you download and extract Maven to the home directory and you want to add its bin directory to the PATH. Let’s assume that the absolute path of this bin directory is /home/abhishek/maven/apache-maven-3.8.0/bin. Here’s what you should be doing: export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin
export PATH=$PATH:/home/abhishek/maven/apache-maven-3.8.0/bin
- The $ before a variable name means you are referring to its value. PATH is the variable name, $PATH is the value of the variable PATH.
- You should not use $ with PATH on the left side of =
- There must not be any spaces before and after =
- Don’t forget to include the : after $PATH because the directories in the PATH are separated by a colon.
- There must not be a space before and after the colon (:).
Once you have set the PATH with the new value, please check that the PATH has been correctly updated.
[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/abhishek/maven/apache-maven-3.8.0/bin
You may want to run the command or script for which you modified the PATH. This will tell you for sure if the PATH is correctly set now.
Making the changes to PATH permanent
You added the desired directory to the PATH variable, but the change is temporary. If you exit the terminal, exit the session or log out from the system, the PATH will revert, and the changes will be lost.
If you want to make the changes to the PATH variable permanent for yourself, you can add it to the .bashrc file in your home directory, assuming you are using the Bash shell.
You can use a text editor like Nano or Vim for this task.
If you want the modified PATH variable to be available for everyone on the Linux system, you can add the export to the /etc/profile file. This is suitable when you are a sysadmin and have a configured system with a custom path.
💡See the path in a simplified view
Reading the PATH variable values and figuring out the directories could be complicated for some people. You can use the tr command to format its output to make it more easily readable.
Bonus tip: The directories take precedence in PATH
There are several directories in the PATH variable. When you run an executable file/command, your system looks into the directories in the same order as they are mentioned in the PATH variable.
If /usr/local/sbin comes before /usr/bin, the executable is searched first in /usr/local/sbin. If the executable is found, the search ends and the executable is executed.
This is why you’ll find some examples where the additional directory is added before everything else in PATH:
export PATH=your_directory:$PATH
If you think that your additional directory should be searched before everything else you should add it before the $PATH; otherwise, add it after $PATH.
Was it clear enough?
I highly suggest reading up on the concept of absolute and relative paths in Linux. It will give you a better understanding of the topic.
I have tried to explain things with the necessary details but not going too deep into details. Does it make the topic clear, or are you more confused than before? If you still have doubts, please let me know in the comments.
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.
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 which with whoami :
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"
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.
3. Paste the export syntax at the end of the file.
export PATH="/Directory1:$PATH"
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:
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: )"
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.
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.
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.
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.
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.
How to Add a Directory to PATH in Linux
When you type a command on the command line, you’re basically telling the shell to run an executable file with the given name. In Linux, these executable programs like ls , find , file and others, usually live inside several different directories on your system. Any file with executable permissions stored in these directories can be run from any location. The most common directories that hold executable programs are /bin , /sbin , /usr/sbin , /usr/local/bin and /usr/local/sbin .
But how does the shell knows, what directories to search for executable programs? Does the shell search through the whole filesystem?
The answer is simple. When you type a command, the shell searches through all directories specified in the user $PATH variable for an executable file of that name.
This article shows how to add directories to your $PATH in Linux systems.
What is $PATH in Linux #
The $PATH environmental variable is a colon-delimited list of directories that tells the shell which directories to search for executable files.
To check what directories are in your $PATH , you can use either the printenv or echo command:
The output will look something like this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
If you have two executable files sharing the same name located in two different directories, the shell will run the file that is in the directory that comes first in the $PATH .
Adding a Directory to your $PATH #
There are situations where you may want to add other directories to the $PATH variable. For example, some programs may be installed in different locations, or you may want to have a dedicated directory for your personal scripts, but be able to run them without specifying the absolute path to the executable files. To do this, you simply need to add the directory to your $PATH .
Let’s say you have a directory called bin located in your Home directory in which you keep your shell scripts. To add the directory to your $PATH type in:
The export command will export the modified variable to the shell child process environments.
You can now run your scripts by typing the executable script name without needing to specify the full path to the file.
However, this change is only temporary and valid only in the current shell session.
To make the change permanent, you need to define the $PATH variable in the shell configuration files. In most Linux distributions when you start a new session, environment variables are read from the following files:
- Global shell specific configuration files such as /etc/environment and /etc/profile . Use this file if you want the new directory to be added to all system users $PATH .
- Per-user shell specific configuration files. For example, if you are using Bash, you can set the $PATH variable in the ~/.bashrc file. If you are using Zsh the file name is ~/.zshrc .
In this example, we’ll set the variable in the ~/.bashrc file. Open the file with your text editor and add the following line at the end of it:
Save the file and load the new $PATH into the current shell session using the source command:
To confirm that the directory was successfully added, print the value of your $PATH by typing:
Conclusion #
Adding new directories to your user or global $PATH variable is pretty simple. This allows you to execute commands and scripts stored on nonstandard locations without needing to type the full path to the executable.
The same instructions apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.
Feel free to leave a comment if you have any questions.