UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 1.9 Which Shell Am I Running? Chapter 1
Introduction
Next: 1.11 How the Shell Executes Other Commands
 

1.10 Internal and External Commands

Some commands that you type are internal, built into the shell. For example, the cd command is built-in. That is, the shell interprets that command and changes your current directory (1.21) for you. The ls command, on the other hand, is an external program stored in the file /bin/ls.

The shell doesn't start a separate process to run internal commands. External commands require the shell to fork and exec (1.11) a new subprocess (38.3); this takes some time, especially on a busy system. (Article 7.4 shows an example where extra speed can be important.)

When you type the name of a command, the shell first checks to see if it is a built-in command and, if so, executes it. If the command name is an absolute pathname (1.21) beginning with /, like /bin/ls, there is no problem: the command is likewise executed. If the command is neither built-in, nor specified with an absolute pathname, the shell looks in its search path (8.7) for an executable program or script with the given name.

The search path is exactly what its name implies: a list of directories that the shell should look through for a command whose name matches what is typed.

The search path isn't built into the shell; it's something you specify in your shell setup files (2.2).

By tradition (21.4), UNIX system programs are kept in directories called /bin and /usr/bin, with additional programs usually used only by system administrators in /etc and /usr/etc. Many versions of UNIX also have programs stored in /usr/ucb (named after the University of California at Berkeley, where many UNIX programs were written). There may be other directories containing programs. For example, the programs that make up the X Window System (1.31) are stored in /usr/bin/X11. Users or sites often also have their own directories where custom commands and scripts are kept, such as /usr/local/bin.

The search path is stored in an environment variable (6.1) called PATH (6.4). A typical PATH setting might look something like this:

PATH=/bin:/usr/bin:/usr/bin/X11:/usr/ucb:/home/tim/bin:

The path is searched in order, so if there are two commands with the same name, the one that is found first in the path will be executed.

You can add new directories to your search path (8.7) on the fly, but the path is usually set in shell setup files.

- TOR


Previous: 1.9 Which Shell Am I Running? UNIX Power ToolsNext: 1.11 How the Shell Executes Other Commands
1.9 Which Shell Am I Running? Book Index1.11 How the Shell Executes Other Commands

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System