How to Add a Directory to PATH in Linux?

Linux is CLI based operating system that stores everything as files. A system contains executable files in specific directories for every program; when a user inputs a command such as “ls” or “pwd”, the system initiates a program. The program searches for the executable files, allowing the system to perform a specific task for that program.  

The common directories which contain the executable user files are “/bin”, “/usr/bin” and “/usr/local/bin”. While the system executable files are stored in “/sbin”, “/usr/sbin” and “/usr/local/sbin” directories which are specified in the “$PATH” variable.

This guide will cover the methods to add a directory to $PATH in Linux with this supporting content:

Let’s begin with a basic understanding of the $PATH variable and how to check the $PATH variable for a specific command.

What is $PATH and How to Check the $PATH of a Command?

Linux shell has a predefined variable called PATH, which contains directories with their absolute paths. The $PATH is a colon-separated list that tells the shell to look for the commands in these executable files.

When a user runs the commands, its related program is initiated, which determines which files to run to execute the command based on the $PATH variable. The below command can be utilized to find out all the directories in the $PATH variable:

$ echo $PATH

The above output shows the list of all the directories with executable files.

The “printenv” command can also be used to print the environment variables in the system:

$ printenv PATH

If two executable files have the same name sharing separate directories, the Linux shell will execute the file which comes first in the $PATH variable. 

To know any command’s executable path, the “which” keyword is used. For instance, to know the “pwd” directory where this command’s executable files are present, use this command:

$ which pwd

The executable files for the “pwd” command are present in the “/usr/bin/pwd” directory.

How to Add a Directory to $PATH in Linux?

The directories paths added within the $PATH variable can be executed from anywhere in the system without providing the absolute path. To add a directory to the $PATH variable in Linux, we can simply export that path to the $PATH variable, and the directory can be added permanently or temporarily.

The shell configuration startup file is used to add the $PATH variable for a specific directory permanently. To add the $PATH variable of a directory for all the users in the system, the shell configuration files “/etc/profile” or “/etc/environment” are utilized, also known as Global $PATH variables.    

Every shell has its specific configuration files for a specific user only; for instance, the Bash shell users can add the $PATH variable to the “~/.bashrc” file, while the “~/.zshrc” file is used to add the $PATH variable in the Z shell.

We can add a directory to $PATH either permanently or temporarily for the current secession only in Linux. The next section will explain both methods.

Add a Directory to $PATH Permanently

In this guide; the bash script will be used. To permanently add the $PATH variable for a directory at home named “Automation”, the shell configuration startup file “bashrc” is used. To add a specific directory to the $PATH variable, use the following steps:

Step 1: Open Startup File bashrc

As the bashrc file is used for adding the $PATH variable in Bash Shell, first open the “bashrc” file in the nano editor utilizing the following command:

$ nano ~/.bashrc

Step 2: Add the Desired Directory PATH to bashrc File

After opening the “~/.bahsrc” file, scroll down at the end of the file and add the below-mentioned export PATH command, which will add the modified PATH variable to the Shell Environment variable:

$ export PATH="$HOME/Automation:$PATH"

Press the “CTRL + O” keys to save the changes. Then Press “Enter” for keeping the file name the same and use the “Ctrl + X” keys to exit the editor.

Step 3: Save the Changes

To save the changes to the currently logged-in shell session, the below source command is used:

$ source ~/.bashrc

Alternatively, you can use the following command to add the directory to the PATH permanently:

$ echo 'export PATH=$PATH:/path/to/directory' >> ~/.bashrc

Step 4: Verify the Desired Directory is Added to $PATH

To verify that the desired directory is added successfully to the $PATH variable, use the below command:

$ echo $PATH

The output shows that the $PATH variable has currently added directory “/home/ubuntu/Automation”, which will be automatically executed every time you start a new terminal session.

Add a Directory to $PATH Temporarily

Sometimes; the directory needs to be added to $PATH only for the current session, which expires when we close the terminal. To add the directory for the currently logged-in session only, we simply need to run the below export command in the terminal:

$ export PATH="$HOME/Automation:$PATH"

It will add the “$HOME/Automation” directory to the $PATH variable, which can be used for the current session.

Conclusion

To add a directory to PATH in Linux, we can add that directory path to its Shell configuration file. To add a directory permanently in the Bash shell, the directory path will be added to the “bashrc” file, while for the Z shell, the “zshrc” file is used. For adding a directory temporarily, we can simply run the “export” command in the terminal. The same steps for adding a directory to the $PATH variable can be used for other Linux distributions, such as RHEL, Debian-based, Cent-OS, and Linux Mint.