Programs and Paths

When you type the name of a program, such as tr to the shell, or mention the name of a program in a shell script, the shell looks for the program to execute in a list of directories known as a "path". The first executable file with the correct name will be executed.

To see what your path is, type to the shell:

echo $path

This prints the value of the path variable.

You can set your path by setting the variable, like this: (Note: we assume here that you are using a C-shell, that is, either csh or tcsh. In the other shells, paths work in a generally similiar way, but the details are different.) set path =(/pkg/ling/bin /usr/bin /bin) The shell does not automatically update the index that it uses to find files when you reset the path variable. To update the index, give the command:

rehash

After executing this command, the shell will look only in these three directories. Any program not found in these cannot be executed by giving just its base name. You can always execute a program by giving its full path name, if you know it. For example, the TeX text formatter is in the directory /pkg/tetex/bin on Babel, so if you have set your path as above, if you just type:

tex
the shell will respond:

tex: command not found.

You could still execute TeX by typing:

/pkg/tetex/bin/tex

There is a command that will tell you where programs are, so long as they are to be found in a standard list of system directories. This is the whereis command. Given as argument the name of a program, it lists the location of executables of that program as well as source files and manual pages. For example:

whereis tex
produces the result:

tex: /usr/local/bin/tex /usr/local/bin/tex.010809

When you set your path by giving a variable setting command as above, the path you set lasts only for the duration of the shell. When you log out, that setting will disappear. To make your path setting persistent, you need to put the path setting command into your shell initialization file. This is the file .cshrc or .tcshrc. This file is executed every time you start up a shell.

Here is the path setting command recommended for students in this course:

set path=(~/bin /pkg/ling/etc/httpd/htdocs/courses/ling538/bin /usr/xpg4/bin /pkg-ling/bin $path .)

Notice that it adds several directories to the existing path ($path). This is because the system manager will have set things up so that you start out with a path containing the directories in which the programs used by most users can be found. Unless you know a lot about which programs are where, you should not explicitly list all of the directories in your path setting command. Instead, just add any special directories that you need.

The first entry in this command, ~/bin is the subdirectory bin in your home directory. (Remember that ~ is an abbreviation for the user's own home directory.) This is the standard place to put your own programs. If you don't already have a bin directory, you may want to create one. Do this by giving the command:

mkdir bin
in your home directory.

The next directory, /pkg/ling/etc/httpd/htdocs/courses/ling538/bin contains programs for this course. We put this ahead of the system directories so that, where more than one version of the same program exists, you will get the one we want for this course.

The next directory, /usr/xpg4/bin contains relatively up-to-date versions of Sun programs that exist in both older and newer versions.

The next directory, /pkg-ling/bin, contains programs specific to the Linguistics department.

The last entry in the path, ., means "the current directory". This is not the directory that you happen to be in, but the current working directory when you execute a program. If you are writing shell scripts or compiling programs, it is convenient to be able to execute them from the directory in which you are working on them. This entry is put last in order to avoid Trojan Horses. A "Trojan Horse" is a malevolent program that is given the same name as a standard program. The idea is that if a cracker can place such a program in a directory from which someone may execute it, the user will inadvertently execute the cracker's program rather than the intended program. Putting the current directory last in the path ensures that if a system program and a program in the current directory have the same name, it is the system program that will be executed.

If you are ever unsure which version of a program you are using, the "which" command (universal on Unix systems) will help you out. It takes as argument the name of a program and reports the full path name of the program that would be executed if you were to type the naked program name. For example, if you type:

which tr
and have not set your path to put the l538 directory before the system directories, the response will probably be:

/bin/tr

If, on the other hand, you have fixed your path, the result will be:

/pkg/ling/etc/httpd/htdocs/courses/ling538/bin

There is also a command (built into the shell, not a separate program) called where, which is similar but reports all instances of its argument in your path, as well as any aliases, in priority order. That is, the first one listed is the one that which would list, the one that would be executed.