Add a dir to PATH
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?
$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:
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:
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:
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
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
$PATHensures that the new definition of
PATHstarts with all the contents of the existing
PATH. In other words, including the
PATHvariable here expands on the variable’s contents instead of overwriting them.
:/etc/custom-directoryadds the new directory. Note the placement of the colon, which is necessary because the
PATHlist is colon-separated.
You can verify the updated contents of
PATH using the
echo command as shown above:
PATH Variable Scope
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
PATHvariable for a given user by adding the
exportcommand 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
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
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.
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.
Small world. Big idea!
- Welcome to visit the knowledge base of SRE and DevOps!
- License under CC BY-NC 4.0
- No personal information is collected
- Made with Material for MkDocs and generative AI tools
- Copyright issue feedback me#imzye.com, replace # with @
- Get latest SRE news and discuss on Discord Channel