How to add directory path to $PATH variable in Linux

When you type a command into a Linux terminal, what’s really happening is that a program is being executed. Normally, to execute a custom program or script, we need to use its full path, such as /path/to/script.sh or just ./script.sh if we’re already in its residing directory. Alternatively, we can execute a lot of commands without specifying paths, like uptime or date, etc.

The reason we don’t need to specify paths for some commands is because of the $PATH variable. This is a variable that can be configured to tell our Linux system where to look for certain programs. That way, when typing date into the terminal, Linux checks the $PATH variable to see a list of directories to look for the program.

In this guide, we’ll see how to add a directory to the $PATH variable on Linux. This will enable you to call on your program or script from anywhere in the system, without needing to specify the path to where you’ve stored it. Follow along with us as we show how to view the directories in $PATH, and add a directory either temporarily or permanently to the variable.

In this tutorial you will learn:

  • How to see currently configured directories in $PATH shell variable
  • How to temporarily add directory to $PATH
  • How to permanently add directory to $PATH
Adding a directory to $PATH on Linux

Adding a directory to $PATH on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

View currently configured directories in $PATH

Seeing all the directories that are currently configured in your system’s $PATH variable is easy. Just use the echo command like this:

$ echo $PATH


Viewing the currently configured directories in our $PATH variable

Viewing the currently configured directories in our $PATH variable

As you can see, there are a few different directories already stored in $PATH. This is what allows us to run so many commands by default, without specifying their full location in the terminal.

To see what directory a command belongs to, you can use the which command.

$ which date
/bin/date

Temporarily add a directory to $PATH

To add a directory to $PATH for the current session, use the following command syntax. In this example, we’re adding the /bin/myscripts directory.

$ export PATH="/bin/myscripts:$PATH"

You can verify afterwards that the directory has been added.

$ echo $PATH
/bin/myscripts [...]

Now, files we have stored in the /bin/myscripts directory can be executed anywhere, without specifying their full path. This configuration will change when we end the current session (reboot the PC or close the terminal). To make it permanent, check out the section below.

Permanently add a directory to $PATH

To add a directory to $PATH permanently, we’ll need to edit the .bashrc file of the user you want to change. Use nano or your favorite text editor to open the file, stored in the home directory.

$ nano ~/.bashrc


At the end of this file, put your new directory that you wish to permanently add to $PATH.

export PATH="/bin/myscripts:$PATH"

Save your changes and exit the file. Afterwards, execute the following command to make the changes take effect in your current session. Alternative, you can log out or reboot the system.

$ source ~/.bashrc

That’s all there is to it. You can check $PATH once more to verify the change.

$ echo $PATH

Closing Thoughts

In this guide, we learned about the $PATH variable and how it controls what commands are able to be executed without specifying their full path. We also saw how to add new programs or scripts to $PATH either temporarily or permanently.



Comments and Discussions
Linux Forum