The PATH Variable

After reading this document, you should know what the PATH variable is, how to set it, and how to view the directories currently therein.

Motivation

Consider the following UNIX terminal session:

 
dbettis@rhino[~]$ ls
...

When you type the command ls, the shell dutifully executes the command and returns the results to you. During the course of a terminal session, you type more commands. These include things like emacs, firefox, so on and so forth. But where do these commands come from? Obviously, they're included when you install the operating system, but where are they?

The UNIX command which will tell you the full path to a binary it's about to execute. For example:

 
dbettis@rhino[~]$ which ls
/bin/ls

That means that the exectuable for the command ls is located in /bin. Alternatively, to run ls, you can type in the full path to the command:

 
dbettis@rhino[~]$ /bin/ls
...

It seems like there's a bit of magic going on here, however. How does the system know that ls is in /bin? The way the system knows is the PATH environment variable!

Envrionment Variables

First, what's an environment variable? It's a variable that persists for the life of a terminal session. Applications running in that session access these variables when they need information about the user. To see a listing of all the environment variables, execute the following:

 
dbettis@rhino[~]$ export
declare -x USER="dbettis"
...

The name of variable is USER and the contents of that variable is dbettis. Another way to see the contents of an environment variable is to do the following:

 
dbettis@rhino[~]$ echo $USER
dbettis

The PATH Environment Variable

The PATH environment variable has a special format. Let's see what it looks like:

 
dbettis@rhino[~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.

It's essentially a :-separated list of directories. When you execute a command, the shell searches through each of these directories, one by one, until it finds a directory where the executable exists. Remember that we found ls in /bin, right? /bin is the second item in the PATH variable. So let's remove /bin from PATH. We can do this by using the export command:

 
dbettis@rhino[~]$ export PATH=/usr/local/bin:/usr/bin:/sbin:/usr/sbin:.

Make sure that the variable is set correctly:

 
dbettis@rhino[~]$ echo $PATH
/usr/local/bin:/usr/bin:/sbin:/usr/sbin:.

Now, if we try to run ls, the shell no longer knows to look in /bin!

 
dbettis@rhino[~]$ ls
-bash: ls: command not found

As expected, ls can no longer be found. Let's add /bin back to PATH, as ls is a very useful thing to have.

 
dbettis@rhino[~]$ export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.

Adding to PATH

There are many times where you'll want to append an item to PATH. First, let's see what the current PATH is:

 
dbettis@rhino[~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.

The way to add a directory is as follows:

 
dbettis@rhino[~]$ export PATH=$PATH:/new/path

This command adds /new/path to PATH. Let's see if it got updated:

 
dbettis@rhino[~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.:/new/path

Making this happen every time you login

There's a special file in your home directory called .bashrc In UNIX, a convention is that files beginning with . are configuration files, and thus should be hidden from view. ls will only list files beginning with a . if passed the -a flag. e.g.

 
dbettis@rhino[~]$ ls -a

At any rate, this file (.bashrc), simply contains a list of commands. Each one of these commands gets executed every time you create a new shell.

 
dbettis@rhino[~]$ cat .bashrc
export PATH="$PATH:/p/firefox/bin"
..

Every time a shell is started, /p/firefox/bin is added to PATH. If you wish to have certain directories automatically added to PATH, simply place those commands at the end of this file. Log out and log back in to view the changes. Alternatively, you can load the contents of that file in the current session:

 
dbettis@rhino[~]$ . .bashrc

Back