Open Source

How to add directories to your $PATH in Linux

When you need to be able to run a Linux tool from a non-standard directory, you need to add that directory to your user $PATH. Here's how to do it.

pathhero.jpg
Image: Jack Wallen

When you type a command in the Linux terminal, the command runs...or so it should. Why? Because most common executables can be found within certain directories (such as /usr/bin, /usr/sbin, /usr/local, etc.). It just so happens those directories are a part of what is called the user $PATH. In fact, if you type the command echo $PATH, you will see every directory that happens to be included in your user $PATH. That means every file with executable permissions can be run from any location. If you happen to be in your home directory, you could issue the command mkdir TEST because the mkdir command is found in the /bin directory (which happens to be in your path).

But what happens when you install an application outside of the default directory structure and the command(s) for that application need to be run globally? What do you do? You add that directory to your user $PATH. Say, for instance, you've found an executable file that you need to use, but that just so happens to be a self-contained application. And what if you wanted to (for whatever reason) run that command from the /opt directory (a directory you won't find in your user $PATH)? You could certainly move that file to the /opt directory and always run the command with /opt/file (where file is the name of the executable file). If you wanted to be able to run file globally, you'd have to add /opt to your user $PATH.

How do you do that?

There are two ways and I am going to show you both.

The temporary addition

The first thing you'll want to do (this is great for testing purposes), is temporarily add the directory to your $PATH. This addition will remain until you reboot your computer. Let's continue with the example above (you want to add /opt). To do this, you would use the following command:

export PATH=$PATH:/opt

Once you've issued the above command, test it with:

echo $PATH

You should now see /opt added to your $PATH (Figure A).

Figure A

Figure A

Before and after /opt was added to the $PATH.

Remember, the second you reboot that machine, the newly added $PATH directory will no longer apply. Let's fix that.

The permanent addition

How you permanently add the new directory to your $PATH will depend upon which shell you use. Most Linux distributions will be running Bash (so we'll concentrate on that shell). To find out if that's your shell, issue the command echo $SHELL. What you should see in return is /bin/bash. Now that we know your shell is, indeed, bash, we know which file to add the $PATH directory into. Open the file ~/.bashrc in your favorite editor and add the following line to the bottom of that file:

export PATH=$PATH:/opt

The above addition would permanently add the /opt directory to your $PATH. Close your terminal window, reopen it, and issue the command echo $PATH and you should see /opt added to the output. If you were to reboot your machine, /opt would remain in the $PATH.

That's all there is to it

Surprise! Adding directories to your user $PATH is actually quite simple. No matter your reason for needing to add directories, you now have the means to do so either on a temporary or permanent basis. Hopefully, this will help make your Linux command line usage a bit easier.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox