X

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.

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.

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"

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

You can verify the changes by displaying the PATH variable.

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.

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.

Categories: Linux & Unix
Karim Buzdar: About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various websites.
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings