Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [97]

By Root 987 0


* * *

command


Usage

command [ -p ] program [ arguments ... ]

Purpose

To bypass the shell's inclusion of functions in the search for commands to run. This allows access to built-in versions of commands from functions with the same name as the built-in command.

Major options

-p

When searching for commands, use a default value of $PATH that is guaranteed to find the system's utilities.

Behavior

command finds the named program by looking for special and regular built-ins, and then searching along $PATH. With the -p option, it uses a default value for $PATH, instead of the current setting.

When program is a special built-in command, any syntax errors do not abort the shell, and any preceding variable assignments do not remain in effect after the command has finished.

Caveats

The command built-in command is not a special built-in command. Woe be to the shell programmer who defines a function named command!

* * *

The POSIX standard provides the following two additional special qualities for the special built-in commands:

A syntax error in a special built-in utility may cause a shell executing that utility to abort, while a syntax error in a regular built-in utility shall not cause a shell executing that utility to abort. [ ... ] If a special built-in utility encountering a syntax error does not abort the shell, its exit value shall be nonzero.

Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be the case with a regular built-in or other utility.

The second item needs some explanation. As mentioned earlier in Section 6.1.1, you can specify a variable assignment at the front of a command, and the variable will have that value in the environment of the executed command only, without affecting the variable in the current shell or subsequent commands:

PATH=/bin:/usr/bin:/usr/ucb awk '...'

However, when such an assignment is used with a special built-in command, the assignment stays in effect from then on, even after the special built-in completes.

Table 7-9 lists several commands not otherwise described in this chapter. Most of them are either specialized, or irrelevant for shell scripting, but to be complete here's a brief description of what they do and when to use them:

alias, unalias

These are used to define and remove aliases, respectively. The shell expands alias definitions when commands are read. Aliases are primarily useful for interactive shells; e.g., alias 'rm=rm -i' to force rm to ask for confirmation. The shell does not do recursive alias expansion, thus this definition is valid.

bg, fg, jobs, kill

These commands are used for job control, an operating system facility by which jobs may be moved in and out of the background.

fc

Short for "fix command," this command is also intended for interactive use. It manages the shell's saved history of previously executed commands, allowing the interactive user to recall a previous command, edit it, and then re-execute it.

This command was originally developed in ksh to provide a feature comparable to the "!-history" mechanism in the BSD C shell, csh. fc is now largely superseded by the interactive command-line editing features of ksh, bash, and zsh.

times

This command prints the CPU time accumulated by the shell and all child processes that it has run so far. It is not particularly useful for day-to-day scripting.

umask

This sets the file permission creation mask, and is discussed in Section B.6.1.3 in Appendix B.

Two remaining commands are useful in scripts. The first is wait, which waits for background programs to finish. With no arguments, wait waits for all background jobs to finish. Otherwise, each argument is either the process ID (see Section 13.2) of a background job, or a job-control job specification.

Finally, the . (dot) command is important. It is used to read and execute commands contained in a separate file. For example, if you have a number of shell functions that you would like to use in multiple scripts, the right

Return Main Page Previous Page Next Page

®Online Book Reader