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.
command -v git >/dev/null || echo "git is not installed" cd() { builtin cd "$@" && echo "now in $PWD"; }
Try it
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.
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
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 tooltowhich, which is not standardized and may print messages or return0for 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) orbuiltin(for builtins), or the function calls itself forever. - Aliases in scripts. Non-interactive shells do not expand aliases by default, but
commandis the safe habit in sourced files used interactively. commanddoes not skip keywords. Reserved words such asifand[[are parsed before lookup.- Special builtins. In POSIX mode,
commandremoves the special properties of special builtins such asexitandexport: a failure no longer aborts the shell. -pis for safety. Use it whenPATHmay have been changed or when you want the system version of a tool.type -ashows all matches.command -vshows only the first, the one that would run.- Path with slash.
command /bin/lsworks, but a name with a slash bypasses lookup anyway.
Related
type,which,alias,hash,exec,unset- Lessons: Functions, Getting help, Portability