Linux path environment variable

The PATH environment variable is an essential component of any Linux system. If you ever use the command line at all, the system is relying on the PATH variable to find the location of the commands you are entering.

In this tutorial, you’ll learn about the PATH environment variable and how it works. You’ll also see how it can be modified by removing paths or adding your own custom directories to the variable.

In this tutorial you will learn:

  • What is the PATH variable and how’s it work?
  • How to temporarily or permanently add a directory to $PATH
  • How to remove a directory from $PATH
Linux path environment variable
Linux path environment variable
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

The Linux PATH Variable

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/ or just ./ if we’re already in its residing directory. Alternatively, we can execute a lot of commands without specifying paths, by simply typing a command 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 a command into the terminal, Linux checks the $PATH variable to see a list of directories to look for the program.

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

Add Directory to PATH Variable

Adding a directory to the PATH variable 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 the steps below to see how to add a directory either temporarily or permanently to the PATH variable.

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

Remove directory from $PATH

You can remove a directory from PATH by editing the appropriate file and removing the undesirable directory. The directories for PATH can be configured in the ~/.bashrc file (per user basis) or the /etc/environment (system wide variables).

As an example, here’s what the /etc/environment file looks like on our test system.

Viewing the /etc/environment file from which we can remove directories from PATH
Viewing the /etc/environment file from which we can remove directories from PATH

Removing directories from here would take effect across the whole system, whereas removing directories from .bashrc will only affect one user.

We elaborate more on these files in our guide on how to set and list environment variables.

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 remove a directory or add new programs or scripts to $PATH either temporarily or permanently.

Comments and Discussions
Linux Forum