Adding a Path to the Linux PATH Variable

1. Overview

In this quick tutorial, we’ll focus on how to add a path to the Unix PATH variable.

2. PATH Variable

The PATH variable is an environment variable that contains an ordered list of paths that Unix will search for executables when running a command. Using these paths means that we do not have to specify an absolute path when running a command.

For example, if we want to print Hello, world!, the command echo can be used rather than /bin/echo so long as /bin is in PATH:

echo "Hello, world!"

Unix traverses the colon-separated paths in order until finding an executable. Thus, Unix uses the first path if two paths contain the desired executable.

We can print the current value of the PATH variable by echoing the PATH environment variable:

echo $PATH

We should see a list of colon-separated paths (exact paths may differ):

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

3. Adding a New Path

We add a new path to the PATH variable using the export command.

To prepend a new path, such as /some/new/path, we reassign the PATH variable with our new path at the beginning of the existing PATH variable (represented by $PATH):

export PATH=/some/new/path:$PATH

To append a new path, we reassign PATH with our new path at the end:

export PATH=$PATH:/some/new/path

4. Persisting Changes

When we use the export command and open a new shell, the added path is lost.

4.1. Locally

To persist our changes for the current user, we add our export command to the end of ~/.profile. If the ~/.profile file doesn’t exist, we should create it using the touch command:

touch ~/.profile

Then we can add our export command to ~/.profile.

Additionally, we need to open a new shell or source our ~/.profile file to reflect the change. We’d either execute:

. ~/.profile

or we could use the source command if we are using Bash:

source ~/.profile

We could also append our export command to ~/.bash_profile if we are using Bash, but our changes will be not be reflected in other shells, such as Z shell (zsh). We shouldn’t add our export command to ~/.bashrc because only interactive Bash shells read this configuration file. If we open a non-interactive shell or a shell other than Bash, our PATH change will not be reflected.

4.2. Globally

We can add a new path for all users on a Unix system by creating a file ending in .sh in /etc/profile.d/ and adding our export command to this file.

For example, we can create a new script file, /etc/profile.d/example.sh, and add the following line to append /some/new/path to the global PATH:

export PATH=$PATH:/some/new/path

All of the scripts in /etc/profile.d/ will be executed when a new shell initializes. Therefore, we need to open a new shell for our global changes to take effect.

We can also add our new path directly to the existing PATH in the /etc/environment file:

PATH=<existing_PATH>:/some/new/path

The /etc/environment file is not a script file—it only contains simple variable assignments—and is less flexible than a script. Because of this, making PATH changes in /etc/environment is discouraged. We recommend adding a new script to /etc/profile.d instead.

5. Conclusion

In this tutorial, we saw how Unix uses the PATH variable to find executables when running a command.

We can prepend or append to PATH, but we must persist these changes in ~/.profile. We can use ~/.bash_profile as well, but ~/.profile is preferred.

We can also change the global PATH value by adding our export command to a new .sh file in /etc/profile.d.