Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Linux command and builtin commands

Reference for the Bash command and builtin builtins: skip aliases and functions, find out whether a command exists with -v, and use a safe default PATH.

When you type a command name, Bash looks for an alias first, then a function, then a builtin, and finally a program in PATH. command and builtin let you skip some of those layers. command NAME runs NAME while ignoring any alias or function with that name. builtin NAME insists on the shell builtin and fails if none exists.

The most important role of command in scripts is not running things but asking questions: command -v NAME tells you whether a command is available, without running it, and in a portable way.

Terminal
command -v git >/dev/null || echo "git is not installed"
cd() { builtin cd "$@" && echo "now in $PWD"; }

Try it

Bash Skipping functions and aliases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Checking for commands with command -v

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
command [-pVv] NAME [ARGUMENT ...]
builtin SHELL-BUILTIN [ARGUMENT ...]
Name Description
NAME Command to run (or describe), skipping aliases and functions.
-v Print how NAME would be resolved: a path for programs, the name for builtins, functions and keywords, the alias definition for aliases. Prints nothing and returns non-zero if not found.
-V A longer description, like type.
-p Search a default PATH that is guaranteed to find the standard utilities.
SHELL-BUILTIN For builtin: the name of a builtin to run.

Returns: the status of the command that ran. command -v returns 0 when found and 1 when not. builtin returns 1 if the name is not a builtin. command returns 127 if the command cannot be found and 126 if it cannot be executed.

Usage notes

  • Portable existence check. Prefer command -v tool to which, which is not standardized and may print messages or return 0 for unknown names on some systems.
  • Wrappers. When you define a function with the same name as the thing it wraps, call the original with command (for programs) or builtin (for builtins), or the function calls itself forever.
  • Aliases in scripts. Non-interactive shells do not expand aliases by default, but command is the safe habit in sourced files used interactively.
  • command does not skip keywords. Reserved words such as if and [[ are parsed before lookup.
  • Special builtins. In POSIX mode, command removes the special properties of special builtins such as exit and export: a failure no longer aborts the shell.
  • -p is for safety. Use it when PATH may have been changed or when you want the system version of a tool.
  • type -a shows all matches. command -v shows only the first, the one that would run.
  • Path with slash. command /bin/ls works, but a name with a slash bypasses lookup anyway.