Posted December 8, 2009 by Spyros in Linux Tips

How to Add a Directory to Your Path Environment Variable in Linux


The linux shell is probably the most important thing that provides raw power to the end user. Getting to know more about it is crucial if we seek to be able to do simple and more complicated tasks with its help. The bash shell, which is what 99% of linux users use, contains a certain environment that a user works under.

These variables are responsible for letting the shell know of some configuration which, in the end, only makes our lives easier. The core of environment variables is certainly the $PATH variable. In case that you are not familiar with that, $PATH is a variable that helps the shell run binaries under different filepaths. For instance, think of executing “ls -l”. Ls is a command that lies under the path /bin/ls. Now, when we execute that command we should be doing it like “/bin/ls -l”, but in reality we only use “ls -l” to do so. So, what happens ? How is the shell able to get to the location where ls lies on ?

The answer is that it uses the $PATH variable to do so. Let’s print out its contents by executing a simple “echo $PATH” command :

nemesys:darkfight hthought$ echo $PATH

This is the $PATH on my mac computer, if you own a linux or other unix based system, it will be a bit different, but the same rules still apply. Notice that $PATH consists of a series of different filepaths, separated by a colon. Do you spot the “/bin/” filepath ? It’s in the middle of those specified. What this means now is that for every command that we try to execute under a linux shell, it first tries to locate that command in every one of those filepaths and the current path where it was executed from. Therefore, if we execute “ls -l”, the shell is able to tell that we actually want to run “/bin/ls -l”. If we explicitly wanted to execute a binary named ls under our current working dircctory, we would be using a command like “./ls” and not “ls”. The former would execute the ls binary under the current working directory (which you can get with the command pwd).

Adding a Directory to Your $PATH

Sometimes, you may want to add a certain directory to your shell $PATH. It could be that you just want to make your life easier when executing binaries inside a certain directory. For example, you may be a python developer and want to be able to execute python inside a certain folder, without having to type the whole path every time. I have to state at that point that it should not be essential to change your filepath at most cases, but in the case that you want to do it, it’s good to know how to do it.

There are actually two ways to do that. The temporary and the persistent way. The temporary one applies to a certain shell session. Thus, you just change the $PATH variable to whatever you would like it to be. In this way, the updated $PATH variable only applies to your current shell session, meaning that when you close the shell, your previous $PATH becomes as it was before your intervention. In order to do that, you simply execute a command like :


Now, to make this change global, you need to edit your .bash_profile file and include the same command, along with a simple export command like:


export PATH

Finally, if you want to make this change applicable to all users at once, just insert it at /etc/profile.