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 [arg ...]
command [-p] command-name
command -pVv

PARAMETERS

-p
    Use default PATH for standard utilities

-V
    Print version description of command

-v
    Print path or description of command

DESCRIPTION

command is a shell builtin in Bash and other POSIX shells that executes the specified command, ignoring any shell function with the same name. This prevents custom functions from overriding standard utilities or builtins, ensuring the intended program runs. For example, if a function named grep exists, grep pattern file calls the function, but command grep pattern file invokes the system's grep.

The -p option uses a default PATH (typically /usr/bin:/bin:/usr/sbin:/sbin) to locate standard utilities, enhancing script portability without depending on user PATH settings. -v prints the path or description of the command (e.g., builtin or file), while -V shows version information. These are invaluable for debugging and verification in scripts.

As a POSIX-standard feature, command promotes reliable scripting across environments. It searches PATH for non-builtins and functions only as a last resort after options. Exit status matches the invoked command or indicates errors like command not found (127). Widely used in makefiles, init scripts, and automation to avoid function interference.

CAVEATS

command bypasses functions but not aliases; escape with \ or quote to ignore aliases. POSIX-compliant but shell-specific behavior may vary.

EXAMPLES

command ls # Ignores ls function, runs binary
command -v ls # /bin/ls
command -p grep foo # Uses default PATH
command -V echo # echo: shell builtin

EXIT STATUS

0 if command succeeds; 126 if not executable; 127 if not found; 1+ on errors.

HISTORY

Originated in early Bourne shell derivatives like ksh; standardized in POSIX.1-2008 for portability. Bash includes it since version 1.0 (1989), with refinements for builtins and PATH handling.

SEE ALSO

type(1), which(1), whereis(1), bash(1)

Copied to clipboard