command Command

Purpose

Executes a simple command.

Syntax

command [ -p ] CommandNameArgument ... ]

command-v | -V ] CommandName

Description

The command command causes the shell to treat the specified command and arguments as a simple command, suppressing shell function lookup.

Normally, when a / (slash) does not precede a command (indicating a specific path), the shell locates a command by searching the following categories:

  1. special shell built-ins
  2. shell functions
  3. regular shell built-ins
  4. PATH environment variable

For example, if there is a function with the same name as a regular built-in, the system uses the function. The command command allows you to call a command that has the same name as a function and get the simple command.

The command -v and command -V commands write to standard output what path name will be used by the shell and how the shell interprets the command type (built-in, function, alias, and so forth). Since the -v and -V flags produce output in relation to the current shell environment, the command command is provided as a Korn shell or POSIX shell regular built-in command. The /usr/bin/command command might not produce correct results, because it is called in a subshell or separate command execution environment,. In the following example the shell is unable to identify aliases, subroutines, or special shell commands:

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

Flags

Item Description
-p Performs the command search using a default value for the PATH environment variable that is finds all of the standard commands.
-v Writes to standard output the path name used by the current shell to invoke the specified command, according to the following conventions:
  • Commands, regular built-in commands, commands including a / (slash), and any implementation-provided functions found by the PATH environment variable are written as absolute path names.
  • Shell functions, special built-in commands, regular built-in commands not associated with a PATH environment variable search, and shell reserved words are written as just their names.
  • Aliases are identified as such, and their definitions are included in the string.

If the specified command name cannot be found, no output is written and the exit status returns a >0 value.

-V Writes to standard output the command name that will be interpreted by the current shell environment. Although the format of this output is unspecified, The output indicates in which of the following categories the command falls:
  • Commands, regular shell commands, and any implementation-provided subroutines found using the PATH environment variable are identified as such and written as absolute path names.
  • Other shell functions are identified as functions.
  • Aliases are identified as such, and their definitions are included in the string.
  • Special built-in commands are identified as such.
  • Regular built-in commands not associated with the PATH environment variable search are identified as such.
  • Shell reserved words are identified as such.

Exit Status

When the -v or -V flag is specified, the following exit values are returned:

Item Description
0 Successful completion.
>0 The command specified with the CommandName parameter could not be found or an error occurred.

When the -v or -V flag is not specified, the following exit values are returned:

Item Description
126 The command specified by the CommandName parameter was found but could not be invoked.
127 An error occurred in the command command, or the command specified by the CommandName parameter could not be found.

Otherwise, the command command returns the exit status associated with the command specified by the CommandName parameter.

Examples

  1. To make a version of the cd command that prints out the new working directory whenever you change directories, enter:
    cd () {
            command cd "$@" >/dev/null
            pwd
    }
  2. To start off a secure shell script, one in which the script avoids being spoofed by its parent, enter:
    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 the PATH environment variable, the script has the ability to ensure that any command it calls is the intended one.

Files

Item Description
/usr/bin/ksh Contains the Korn shell command built-in command.
/usr/bin/command Contains the command command.