LinuxCommandLibrary

command

Execute a command bypassing shell built-ins

TLDR

Execute the ls program literally, even if an ls alias exists

$ command [ls]
copy

Find and execute a command using a default $PATH (/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc) that guarantees to find all standard utilities
$ command -p [command_name]
copy

Display the path to the executable or the alias definition of a specific command
$ command -v [command_name]
copy

SYNOPSIS

command [-pVv] command_name [arg ...]
command -V command_name
command -v command_name
command -p command_name

PARAMETERS

-p
    Searches the PATH using a default value that is guaranteed to find all standard utilities. This is particularly useful in scripts to ensure that standard utilities are found even if the user has a modified or incomplete PATH.

-v
    Prints a description of command_name, indicating how it would be interpreted. For example, it might show if it's an alias, function, built-in, or executable, and its location. This is similar to the type command, but with a more concise output.

-V
    Prints a more verbose description of command_name than -v. This output often includes the exact path for executables, the definition for functions, or the alias expansion.

--
    Indicates the end of options. Any arguments following -- are treated as command_name and its arguments, even if they start with a hyphen. This is useful when command_name itself might look like an option.

DESCRIPTION

command is a shell built-in that executes a command by searching for it only in the system's PATH for executables, or as a shell built-in, explicitly ignoring shell aliases and functions of the same name.

This is crucial for writing robust and predictable shell scripts, especially when the script needs to call the "true" or original version of a utility that might have been aliased or overridden by a shell function in the user's environment.

For instance, if ls is aliased to ls --color=auto, command ls will execute the original ls binary found in PATH. It also provides options to query the type and location of a command, similar to type or which. The primary goal is to ensure a command is executed in a predictable manner, free from potential user-defined overrides.

CAVEATS

The command utility is a shell built-in, meaning its exact behavior and available options might have minor variations across different shells (e.g., Bash, Zsh, Ksh). However, its core functionality, especially for the -p, -v, and -V options, is consistent with the POSIX standard.

It cannot be used to execute shell functions or aliases directly by name; its purpose is specifically to bypass them and find the underlying executable or built-in.

Using command does not modify the shell's execution environment or PATH permanently. It only affects the command lookup for that single execution.

RETURN STATUS

The exit status of command is that of the command_name if it is found and executed. If command_name is not found, or if an error occurs, command returns a non-zero status (typically 127 for command not found, or other non-zero values for syntax errors).

PURPOSE IN SCRIPTING

command is an indispensable tool for robust scripting. It allows script writers to protect against unintended side effects from user configurations, ensuring that critical system utilities are called in their original form. This is vital for system maintenance scripts, installation scripts, or any script that needs to be self-contained and predictable regardless of the user's shell environment.

HISTORY

The command utility was introduced as part of the POSIX standard for shell utilities. Its primary motivation was to address the challenge of reliable command invocation within shell scripts.

Before command, it was difficult for scripts to guarantee that they were executing a specific external utility rather than a user-defined alias or function, which could lead to unpredictable script behavior or security vulnerabilities.

It provides a standardized, portable mechanism for scripts to force the shell to perform a PATH search and execute the first match, ensuring predictable behavior across various Unix-like systems conforming to POSIX.

SEE ALSO

type(1), which(1), alias(1), builtin(1), exec(1), sh(1)

Copied to clipboard