Select Page


Consider the following UNIX terminal session:

[email protected][~]$ 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 emacsfirefox, 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:

[email protected][~]$ which 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:

[email protected][~]$ /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:

[email protected][~]$ 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:

[email protected][~]$ echo $USER

The PATH Environment Variable

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

[email protected][~]$ echo $PATH

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:

[email protected][~]$ export PATH=/usr/local/bin:/usr/bin:/sbin:/usr/sbin:.

Make sure that the variable is set correctly:

[email protected][~]$ echo $PATH

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

[email protected][~]$ 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.

[email protected][~]$ 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:

[email protected][~]$ echo $PATH

The way to add a directory is as follows:

[email protected][~]$ export PATH=$PATH:/new/path

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

[email protected][~]$ echo $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.

[email protected][~]$ 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.

[email protected][~]$ 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:

[email protected][~]$ . .bashrc