Skip to content

Add a dir to PATH

homepage-banner

Have you ever wondered how certain executables on Linux can be accessed as simple commands from the command line? Have you ever wanted to run a program on Linux without having to provide the entire path?

These are the problems that the PATH variable is designed to solve. In this tutorial, you’ll learn more about what the PATH variable is and how it works. You’ll also see how you can add your own directories to the PATH, allowing you to run programs as simple commands.

What Is the PATH Variable?

The $PATH environmental variable contains a colon-separated list of directories. These directories are where Linux looks for executables, allowing you to run them using only their names.

For example, suppose you have an executable named program-to-run in the /usr/local/bin directory. If that directory is not in your PATH, you will need to run the executable with:

/usr/local/bin/program-to-run

Alternatively, you can change into the directory first and then run the executable:

cd /usr/local/bin
./program-to-run

However, if you have the directory containing the executable in your PATH, you can run it by simply typing its name as a command:

program-to-run

The PATH variable is particularly useful when running specialized developer or system administrator tools, as well as in-development applications. It enables you to run tools and applications efficiently, while keeping them stored wherever best suits your needs.

View the PATH Variable

On a Linux system, it can be helpful to know which directories are already assigned to the PATH. You can easily do this using the echo command, as shown below:

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

As you can see from the output above, Linux systems come with several convenient directories already on the PATH by default.

However, sometimes these directories may not meet your needs. In such cases, you should consider customizing the PATH variable’s contents.

How to Add a Directory to the PATH

Linux comes with several directories in the PATH by default, as shown in the output above. In most cases, these directories are enough. When you install a program on Linux, its executables are usually placed in one of the default directories, making them easy to start using right away.

However, sometimes this is not the case. The PATH variable does not include executables that you download or create and store in non-default directories. But this kind of storage is often necessary when using one-off programs, working with particular system administrator tools, or developing executables.

In cases like this, you may want to add additional directories to the PATH variable to make executables easier to work with.

To do so, use the export command. Here is an example that adds the /etc/custom-directory directory to the PATH:

export PATH="$PATH:/etc/custom-directory"

After running the above command, you can run executables stored in the custom-directory by simply providing the executable names as commands.

Here’s a breakdown of how the export command achieves this:

  • export PATH= begins the definition of the PATH variable.
  • $PATH ensures that the new definition of PATH starts with all the contents of the existing PATH. In other words, including the PATH variable here expands on the variable’s contents instead of overwriting them.
  • :/etc/custom-directory adds the new directory. Note the placement of the colon, which is necessary because the PATH list is colon-separated.

You can verify the updated contents of PATH using the echo command as shown above:

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/etc/custom-directory

PATH Variable Scope

Executing the export command above only updates the PATH variable for the current session. Logging out and logging back into your Linux system results in the PATH being reset.

There are two methods available should you want to have your directory added to the PATH in a more permanent way.

  • You can alter the PATH variable for a given user by adding the export command to that user’s shell configuration file. The location of the configuration file varies depending on the shell program. For Bash, the configuration file is typically ~/.bashrc:

    File: ~/.bashrc

    export PATH="$PATH:/etc/custom-directory"
    

You can modify the global PATH variable for your Linux system by adding the export command to your system’s configuration file, which is typically located at /etc/profile.

However, it is usually preferable to only modify the PATH for the current user. You should only modify the global PATH when you have a directory with executables that are needed by most or all users on the system.

In either case, the result is the same as using the plain export command described above. These options function in the same way, but they cause the extended PATH to persist between sessions.

Conclusion

This tutorial has given you the tools to start using the PATH variable effectively. It not only explains what the PATH variable is and what it does, but also shows you how to add directories to it. Although the change may be simple, adding directories to the PATH can make your life easier and help your tasks run more smoothly.

Leave a message