command

Run a command, bypassing shell functions.

Source: src/scripting/command_builtin.f90:143-257

Synopsis

command [-pVv] command [arguments ...]

Description

The command builtin executes a command, ignoring any shell functions with the same name. This is useful when you've defined a function that wraps a command but need to call the original.

Options

OptionDescription
-pUse default PATH to find command
-vPrint command description (like type -t)
-VPrint verbose command description (like type)

Usage

Bypass Function

# Define wrapper function
ls() {
    command ls --color=auto "$@"
}

# Inside function, 'command ls' calls real ls
# Without 'command', it would recurse infinitely

Default PATH

command -p ls
# Uses default system PATH, ignoring user's PATH

Check Command

command -v git
# /usr/bin/git

command -v cd
# cd

Verbose Check

command -V ls
# ls is /usr/bin/ls
# or
# ls is aliased to 'ls --color'

Examples

Wrapper Functions

# Safe rm wrapper
rm() {
    command rm -i "$@"
}

# Colorized ls
ls() {
    command ls --color=auto "$@"
}

# Git with defaults
git() {
    command git -c user.name="Default" "$@"
}

Check If Command Exists

if command -v docker &>/dev/null; then
    echo "Docker is installed"
fi

POSIX-Compliant Check

# Preferred over 'which'
has_command() {
    command -v "$1" &>/dev/null
}

if has_command wget; then
    wget "$url"
elif has_command curl; then
    curl -O "$url"
fi

Find Standard Command

# Get standard utility, ignoring aliases/functions
command -p cat /etc/passwd

Differences from Direct Execution

MethodFunctionsAliasesBuiltinsPATH
cmdYesYesYesYes
command cmdNoYesYesYes
command -p cmdNoYesYesDefault
\cmdYesNoYesYes
builtin cmdNoNoYesNo

Implementation

Source: command_builtin.f90:143-257

When -p is used, the default system PATH is used instead of $PATH:

  • /usr/bin:/bin:/usr/sbin:/sbin

The -v and -V flags provide command type information similar to type.

Exit Status

StatusCondition
0Command found (with -v/-V) or executed successfully
127Command not found
OtherExit status of executed command

Common Patterns

Safe Wrapper Template

cmd_name() {
    local args=()
    # Add default arguments
    args+=(--some-default)
    args+=("$@")
    command cmd_name "${args[@]}"
}

Check Multiple Commands

require() {
    for cmd in "$@"; do
        command -v "$cmd" &>/dev/null || {
            echo "Required: $cmd" >&2
            return 1
        }
    done
}

require git make gcc || exit 1

See Also

  • type - Show command type
  • builtin - Run builtin only
  • exec - Replace shell with command