After reading this document, you should know what the PATH variable is, how to set it, and how to view the directories currently therein.
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
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/lsThat means that the exectuable for the command
lsis 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!
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
USERand 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
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
/binis the second item in the PATH variable. So let's remove
PATH. We can do this by using the
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
dbettis@rhino[~]$ ls -bash: ls: command not foundAs expected,
lscan no longer be found. Let's add
lsis a very useful thing to have.
dbettis@rhino[~]$ export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.
PATH. First, let's see what the current
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/pathThis command adds
PATH. Let's see if it got updated:
dbettis@rhino[~]$ echo $PATH /usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.:/new/path
There's a special file in your home directory called
.bashrc In UNIX, a convention is that files beginning
. are configuration files, and thus should be hidden
ls will only list files beginning with a
. if passed the
-a flag. e.g.
dbettis@rhino[~]$ ls -aAt 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
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
dbettis@rhino[~]$ . .bashrcBack