Greg Hinkel's UNIX Tip of the Week for March 10, 1996
Understanding your "path" variable.
Each shell has a means of setting variables to make navigation and use
a bit easier. One of those variables is "path." The shells use the path
variable to look for programs to execute. The path variable contains a
string (or list) of directories to be searched. So, when you type
ls
at the system prompt, your shell looks at your path variable (if it's set)
and traverses the entries in your path looking for a directory that contains
a file named "ls." Note that the directories listed at the beginning of
your path will be searched before those at the end. Therefore, it is
generally best to put (list) the "system" directories before any "personal"
directories. Why? you ask! Because, most commands that you execute are
"system" commands, like ls, more, cat, etc. By searching the system
directories first, the shell will find the command quicker, thus resulting in
quicker response for you. As a side note, searching the system directories
first will help prevent you from running Trojan Horse type programs.
Usually, you want to set your path in one of your "login" startup files,
such as .cshrc (for C Shell users) or .profile (for Bourne and Korn Shell
users). This way it gets set every time you log in.
So, an example.
C Shell
set path = (/usr/ucb /usr/bin /usr/local{/X11R6/bin,/bin,,} .)
Bourne and Korn Shell
PATH=/usr/ucb:/usr/bin:/usr/local/X11R6/bin:/usr/local/bin:/usr/local:.
Now, here's what happens if we enter the command "cat .login"
The shell will first look in the directory /usr/ucb to see if a file named
"cat" is in there. On most machines it will not find it there. It will then
look in the directory /usr/bin. On most machines this is where it will find
it. So, it will then execute the program /usr/bin/cat, which as you know, will
display the .login file.
Now, a question might be "How do I know what the system directories are?"
Well, unfortunately, the system directories vary from OS to OS. But, you
generally have things like /bin, /usr/bin, /sbin, /usr/sbin, /usr/ucb,
/usr/bsd, /etc, and others. One thing you can do is use the command "whereis"
to find out where system commands are. So, you could enter "whereis ls" to
see where "ls" is. I often do this when I have to go to an OS that I haven't
met before, cause sometimes my "default" path is insufficient. However, you
may need to find out where "whereis" first. A look at the man page (man
whereis) may help. The "whereis" command knows where the system directories
are, and it only searches the system directories. You should also ask your
system administrator and/or other local users to see if there are "local"
directories that you should include in your path.
You can also execute the command "which" to determine which command you would
execute IF you were to enter the command. For example "which cat" would
return "/usr/bin/cat." The "which" command looks through your path and stops
at the first directory where it finds a file with that name.
Another note. Don't include non-existent directories in your path, this just
slows down the search.
Previous Tip of the Week ·
Next Tip of the Week ·
Index
Greg's Home Page
Greg Hinkel / (hinkelgc AT ornl.gov)
Last Modified: Friday, 02-Sep-2005 12:51:55 EDT
Visitors: 5666 since March 18, 1996