command(1)


command -- execute a simple command

Synopsis

/usr/bin/command [-p] command_name [argument ...]

/usr/bin/command [-v | -V] command_name

Description

This shell script executes the builtin command of the same name as implemented by the /u95/bin/sh shell. See ksh(1) for more information on this shell.

The command command causes the shell to treat the given command_name as a simple command, suppressing the shell function lookup that is described on the ksh(1) manual page.

If the command_name is the same as the name of one of the shell's special built-in utilities, the special properties in the enumerated list at the beginning of will not occur. In every other respect, if command_name is not the name of a shell function, the effect of the command line will be the same as if the command_name were specified without command.

The command utility also provides information concerning how a command name will be interpreted by the shell; see ``Flags''.

Flags

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 ksh(1)), to invoke command_name.

Utilities, regular built-in utilities, command_names including a slash character, and any implementation-provided functions that are found using the PATH variable, 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. This string indicates in which of the following categories command_name falls and include the information stated:

Utilities, regular built-in utilities, and any implementation-provided functions that are found using the PATH variable, 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.

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 simple command or a special built-in command.

Environment variables

The following environment variables affect the execution of command:

LANG
Provide a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-specific 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.

PATH
Determine the search path used during the command search, except as described under the -p option.

Exit codes

When the -v or -V options are specified, an exit value of 0 indicates successful completion; a return value greater than 0 indicates that the command_name could not be found or an error occurred.

In all other cases, 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.

If there is no error in the command utility, and the command_name utility is found and executed, the exit status of command is that of the command_name specified.

Diagnostics

The command(1), env(1), nohup(1), time(1), and xargs(1) utilities 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.

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.

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(1); 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.

Since the -v and -V options of command produce output in relation to the current shell execution environment, if command 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. If a standard utility is implemented as a regular built-in (such as true), the command:

command -v true

yields /bin/true as its output. Other implementation-provided utilities that exist only as built-ins and have no pathname associated with them 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 forth.

Examples

Make a version of cd that always prints out the new working directory exactly once:

cd() { command cd "$@" >/dev/null pwd }

Start off a ``secure shell script'' in which the script avoids being spoofed by its parent:

   # Set IFS to its default value.
   IFS='
   '
   # The preceding value should be <space><tab><newline>.
   # Unset all possible aliases.
   ias -a
   # Note that unalias is escaped to prevent an alias
   # being used for unalias.
   # Ensure command is not a user function.
   unset -f command
   # Put on a reliable PATH prefix.
   PATH="$(command -p getconf _CS_PATH):$PATH"
   # Remainder of script follows.
   . . .
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. 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.

References

ksh(1), type(1)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004