Special File Types   «Prev  Next»
Lesson 7Search paths and commands
ObjectiveDetermine where a command is located and modify the search path to use the command.

Search paths | Commands

Almost all UNIX commands are executable programs, and invoking a command in a shell almost always means running that executable program. Therefore, all the concepts we have discussed regarding file permissions and process ownership apply to commands. You should remember a few additional ideas when working with interactive commands.

Search Path

As you know, when a command name is typed to a shell, the shell uses its search path to locate the corresponding executable file. The search path is set in the system-wide login file (usually /etc/login or /etc/profile) by the use of the PATH environment variable[1]. Users may modify the search path in their personal login scripts.
The simplest way to view the search path is

echo $PATH

This command displays the default search path of the current shell. Notice that you must use $ in front of PATH. This is standard when referring to environment variables in commands.
The search path can be modified in the following way:
PATH=$PATH:/usr/sbin
export PATH

The first line adds the directory /usr/sbin to the end of the search path as defined in the PATH environment variable. The second line makes this change global by exporting the path to the system-wide environment variable.

Administrative commands

Many of the administrative commands are located in /sbin and /usr/sbin, and these directories are not typically included in the search path. When working as an administrator, you may find it convenient to modify your personal search path to include these directories. Otherwise, you will need to give full pathnames to many administrative commands.

Same name, different paths


Confusion can occur when several commands with the same name are located in different parts of the filesystem[2]. For example, some versions of UNIX supply two versions of common commands, one of which takes the BSD[3] family of options, and the other which takes the System V[4] family of options. The BSD-like commands might be in /usr/ucb/bin, whereas the System V commands stay in /usr/bin. Which command gets run when you type, for example, ps? The which command is a handy way to tell:
$ which stty
/usr/ucb/bin/stty

We have also seen the useful whereis command, which tells us which file gets executed, as well as where the source and manual pages for a particular command are located.
$ whereis stty
/usr/ucb/bin/stty /usr/man/man1/stty.1

Search-paths - Exercise

Click the Exercise link to practice your knowledge of search paths and commands.
Search Paths - Exercise

[1]environment variable: An environment variable is a system variable that contains information about some aspect of the system's environment. For example, the PATH environment variable describes the search path that is used whenever a command name is entered without an explicit directory location.
[2]filesystem: A UNIX filesystem is a directory hierarchy. A UNIX system has one overall filesystem, with its root at /, which may be made up of a number of other filesystems.
[3]BSD: BSD is a type of UNIX operating system derived from the work done at Computer Science laboratories at the University of California at Berkeley. The most commonly encountered BSD system is SunOS version 4.3. Berkeley's BSD version 4.4 is also fairly common.
[4]System V','System V is a type of UNIX operating system that arose from an alliance of ATT and Sun. Solaris, AIX, and HP-UX are the most commonly encountered System V operating systems.