The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

command - execute a simple command

 SYNOPSIS



command [-p] command_name [argument ...]

command [ -v | -V ] command_name

 DESCRIPTION

The command utility causes the shell to treat the arguments as a simple command, suppressing the shell function lookup that is described in Command Search and Execution item 1b.

If the command_name is the same as the name of one of the special built-in utilities, the special properties in the enumerated list at the beginning of Special Built-in Utilities will not occur. In every other respect, if command_name is not the name of a function, the effect of command will be the same as omitting command.

The command utility also provides information concerning how a command name will be interpreted by the shell; see -v and -V.

 OPTIONS

The command utility supports the XBD specification, Utility Syntax Guidelines  .

The following options are supported:

-p
Perform the command search using a default value for PATH that is guaranteed to find all of the standard utilities.
-v
Write a string to standard output that indicates the pathname or command that will be used by the shell, in the current shell execution environment (see Shell Execution Environment ), to invoke command_name.
  • Utilities, regular built-in utilities, command_names including a slash character, and any implementation-dependent functions that are found using the PATH variable (as described in Command Search and Execution ), will be written as absolute pathnames.
  • Shell functions, special built-in utilities, regular built-in utilities not associated with a PATH search, and shell reserved words will be written as just their names.
  • An alias will be written as a command line that represents its alias definition.
  • Otherwise, no output will be written and the exit status will reflect that the name was not found.
-V
Write a string to standard output that indicates how the name given in the command_name operand will be interpreted by the shell, in the current shell execution environment (see Shell Execution Environment ). Although the format of this string is unspecified, it will indicate in which of the following categories command_name falls and include the information stated:
  • Utilities, regular built-in utilities, and any implementation-dependent functions that are found using the PATH variable (as described in Command Search and Execution ), will be identified as such and include the absolute pathname in the string.
  • Other shell functions will be identified as functions.
  • Aliases will be identified as aliases and their definitions will be included in the string.
  • Special built-in utilities will be identified as special built-in utilities.
  • Regular built-in utilities not associated with a PATH search will be identified as regular built-in utilities. (The term "regular" need not be used.)
  • Shell reserved words will be identified as reserved words.

 OPERANDS

The following operands are supported:
argument
One of the strings treated as an argument to command_name.
command_name
The name of a utility or a special built-in utility.

 STDIN

Not used.

 INPUT FILES

None.

 ENVIRONMENT VARIABLES

The following environment variables affect the execution of command:
LANG
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
LC_ALL
If set to a non-empty string value, override the values of all the other internationalisation variables.
LC_CTYPE
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).
LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output.
NLSPATH
Determine the location of message catalogues for the processing of LC_MESSAGES .
PATH
Determine the search path used during the command search described in Command Search and Execution , except as described under the -p option.

 ASYNCHRONOUS EVENTS

Default.

 STDOUT

When the -v option is specified, standard output is formatted as:

"%s\n", <pathname or command>

When the -V option is specified, standard output is formatted as:

"%s\n", <unspecified>

 STDERR

Used only for diagnostic messages.

 OUTPUT FILES

None.

 EXTENDED DESCRIPTION

None.

 EXIT STATUS

When the -v or -V options are specified, the following exit values are returned:
0
Successful completion.
>0
The command_name could not be found or an error occurred.

Otherwise, the following exit values are returned:

126
The utility specified by command_name was found but could not be invoked.
127
An error occurred in the command utility or the utility specified by command_name could not be found.

Otherwise, the exit status of command will be that of the simple command specified by the arguments to command.

 CONSEQUENCES OF ERRORS

Default.

 APPLICATION USAGE

The order for command search allows functions to override regular built-ins and path searches. This utility is necessary to allow functions that have the same name as a utility to call the utility (instead of a recursive call to the function).

The system default path is available using getconf; however, since getconf may need to have the PATH set up before it can be called itself, the following can be used:


command -p getconf _CS_PATH

There are some advantages to suppressing the special characteristics of special built-ins on occasion. For example:


command exec > unwritable-file

will not cause a non-interactive script to abort, so that the output status can be checked by the script.

The command, env, nohup, time and xargs utilities have been specified to use exit code 127 if an error occurs so that applications can distinguish "failure to find a utility" from "invoked utility exited with an error indication". The value 127 was chosen because it is not commonly used for other meanings; most utilities use small values for "normal error conditions" and the values above 128 can be confused with termination due to receipt of a signal. The value 126 was chosen in a similar manner to indicate that the utility could be found, but not invoked. Some scripts produce meaningful error messages differentiating the 126 and 127 cases. The distinction between exit codes 126 and 127 is based on KornShell practice that uses 127 when all attempts to exec the utility fail with [ENOENT], and uses 126 when any attempt to exec the utility fails for any other reason.

Since the -v and -V options of command produce output in relation to the current shell execution environment, command is generally provided as a shell regular built-in. If it is called in a subshell or separate utility execution environment, such as one of the following:


(PATH=foo command -v)
 nohup command -v

it will not necessarily produce correct results. For example, when called with nohup or an exec function, in a separate utility execution environment, most implementations will not be able to identify aliases, functions or special built-ins.

Two types of regular built-ins could be encountered on a system and these are described separately by command. The description of command search in Command Search and Execution allows for a standard utility to be implemented as a regular built-in as long as it is found in the appropriate place in a PATH search. So, for example, command -v true might yield /bin/true or some similar pathname. Other implementation-dependent utilities that are not defined by this specification might exist only as built-ins and have no pathname associated with them. These will produce output identified as (regular) built-ins. Applications encountering these will not be able to count on execing them, using them with nohup, overriding them with a different PATH , and so on.

 EXAMPLES

  1. Make a version of cd that always prints out the new working directory exactly once:
    
    cd() {
        command cd "$@" >/dev/null
        pwd
    }
    
    

  2. Start off a "secure shell script" in which the script avoids being spoofed by its parent:
    
    IFS='
    '
    #    The preceding value should be <space><tab><newline>.
    #    Set IFS to its default value.
    
    \unalias -a
    #    Unset all possible aliases.
    #    Note that unalias is escaped to prevent an alias
    #    being used for unalias.
    
    unset -f command
    #    Ensure command is not a user function.
    
    PATH="$(command -p getconf _CS_PATH):$PATH"
    #    Put on a reliable PATH prefix.
    
    #    ...
    
    

    At this point, given correct permissions on the directories called by PATH , the script has the ability to ensure that any utility it calls is the intended one. It is being very cautious because it assumes that implementation extensions may be present that would allow user functions to exist when it is invoked; this capability is not specified by this specification, but it is not prohibited as an extension. For example, the ENV variable precedes the invocation of the script with a user startup script. Such a script could define functions to spoof the application.

 FUTURE DIRECTIONS

None.

 SEE ALSO

sh, type.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]