How to Add Directory to PATH Variable in Linux

What is the PATH variable?

In Linux, executable files reside in many different directories. But you may have noticed that you do not provide a directory path while running your common commands. So you might wonder, how does the Linux system know where the relative executable is.

That's where the PATH variable comes in, the PATH variable stores all paths of the directories where the executables are. So when you issue a command, the Linux system traverses the given directories in the PATH variable until it finds the relevant script/executable.

Why Use PATH?

If you need to run a script often or you are working with multiple scripts and need to manage multiple folders of executable files. It is recommended to use the PATH variables to store the directory path for easy and swift execution.

In this article, you will learn how to add a directory path into the PATH variable and the things you need to consider while adding a directory path into the PATH variable.

Prerequisites

  • Linux-based system
  • Terminal access

Note: The instructions in this article are executed on Linux Mint 20 System, but they are valid for any Linux-based system.

See Current PATH Variable

To see the PATH variable, use the following echo command.

echo $PATH

The $ before PATH indicates that the PATH is a system variable.

Show Linux PATH variable

You can see the directories that are in the PATH variable in the output.

Add Your Directory to the PATH Variable

You can use the following export command to add the desired directory into the PATH variable.

export PATH="<Directory Path>:$PATH"

Note: Remember to insert your directory path in the above command.

Append to PATH

You can run the echo command to verify that the path is added to the PATH variable.

Note: The above command only adds a directory to the PATH variable temporarily and only lasts till you exit the terminal.

To add a directory permanently to the PATH variable, you need to change the shell configuration file, “~/.bashrc”. To make the changes, open the file with the nano editor.

nano ~/.bashrc

Now go to the end of the file and add the following code line in the file.

export PATH="<Directory Path>:$PATH"

Change PATH variable

Press Ctrl+O and then Ctrl+X.

Now either restart the terminal or run the following command in the current terminal for changes to take effect.

source ~/.bashrc

source bashrc

You can verify the changes by displaying the PATH variable.

Show content of new PATH

Note: You need to be careful of the order in which you add directories in the PATH variable. The PATH variable traverses the directory till it finds the executable and it does not look further. So in case, you have updated the script in some other directory. You need to add that directory in front of the previous directory path to run the latest script.

Frequently Asked Questions

What is the PATH variable in Linux?

The PATH variable in Linux is an environment variable that specifies the directories in which the system looks for executable files. When you type a command in the terminal, the system searches these directories to find the executable file for that command.

How can I view my current PATH variable?

To view your current PATH variable, open a terminal and type `echo $PATH`. This command will display the directories listed in your PATH variable, separated by colons.

Why is the PATH variable important?

The PATH variable is crucial because it allows you to run programs and scripts from the terminal without specifying their full path. It simplifies command execution and is essential for the proper functioning of many system and user applications.

How do I add a directory to my PATH variable?

To add a directory to your PATH variable, you can use the command

export PATH=$PATH:/new/directory

Replace /new/directory with the path of the directory you want to add. This change is temporary and lasts only for the current session.

How can I make changes to the PATH variable permanent?

To make changes permanent, you need to add the export command to your shell's startup file, like .bashrc or .bash_profile for Bash. Open the file in a text editor and add

export PATH=$PATH:/new/directory

at the end.

Can modifying the PATH variable be dangerous?

Incorrect modifications to the PATH variable can lead to security risks or system malfunctions. For example, adding an untrusted directory to the PATH can lead to the execution of malicious programs. Always be cautious and understand the implications of changes you make.

How do I remove a directory from the PATH variable?

To remove a directory from the PATH variable, you can manually edit the PATH string and remove the directory's path. This can be done by editing the shell's startup file (e.g., .bashrc) where the PATH is defined.

What is the difference between ~/.bashrc and ~/.bash_profile in relation to PATH?

The file ~/.bashrc is executed for interactive non-login shells, while ~/.bash_profile is executed for login shells. Depending on how you access your terminal, you might need to update one or both files to permanently change your PATH.

How can I check if a specific directory is in my PATH?

To check if a specific directory is in your PATH, use:

echo $PATH

To list all directories in the PATH variable and manually look for the specific directory. Alternatively, you can use a command like:

echo $PATH | grep /directory/path

To search for it directly.

Is the PATH variable case-sensitive?

Yes, the PATH variable is case-sensitive, like most things in Linux. This means that /home/user/bin and /home/user/Bin would be considered different directories. Always ensure the correct case when modifying the PATH.

Conclusion

This article teaches you to add these directories to the PATH variable. If mentioning full paths in commands is a hassle for you and frustrates you to no end. Now you can assign that task to your PATH variable and be done with it.

Leave a Comment