What it does
command is a POSIX shell builtin (commonly in bash, dash, ksh, zsh).
It helps you answer:
- “Does this command exist?”
- “What will my shell execute when I type this?”
- “How do I bypass an alias or function?”
In scripts, command -v is the most reliable, portable way to test for tools.
How it works (mechanical)
When you type a name, the shell resolves it in this general order (varies slightly by shell):
- aliases
- shell functions
- shell builtins
- PATH search (external programs)
command asks the shell to perform its normal resolution logic and report the result —
and optionally to execute the resolved external command while skipping alias/function overrides.
10 Practical Examples
# 1) Check if a command exists (exit 0 if found, non-zero if not) command -v mtr >/dev/null 2>&1
# 2) Print what it resolves to (path, alias, function, builtin) command -v mtr
# 3) Verbose description (bash: more descriptive than -v) command -V ls
# 4) In a script: fail fast if missing if ! command -v mtr >/dev/null 2>&1; then echo "mtr not found. Exiting." exit 1 fi
# 5) Bypass an alias/function and run the external program (common pattern) command ls
# 6) Prefer default PATH search (ignore shell PATH modifications; shell-dependent) command -p ls
# 7) Show the PATH-resolved executable for a tool (portable) MTR="$(command -v mtr)" echo "$MTR"
# 8) Diagnose “why did /usr/bin/command -v fail?” (because command is usually a builtin) type command # output: "command is a shell builtin"
# 9) Compare with which (which is external and can be inconsistent) which mtr command -v mtr
# 10) Avoid alias surprises in scripts (example) # If someone aliased rm='rm -i', this still runs rm normally: command rm -rf /tmp/testdir
Notes & Gotchas
- Usually not /usr/bin/command: on many systems
commandis only a shell builtin. So/usr/bin/command -vfails because that file often doesn’t exist. - Use in scripts: prefer
command -voverwhichfor portability. - -V output varies: the descriptive format differs between shells.
- -p behavior varies: support differs by shell; don’t rely on it unless you know your shell.
You tried
/usr/bin/command -v mtr and got “No such file or directory.”That’s because
command lives inside the shell. Use: command -v mtr (no path).
Historical Context
command is part of POSIX shell behavior and has been present for decades.
It exists specifically because scripts need a predictable way to test for tools and to
bypass interactive shell customizations (aliases/functions) that can break automation.
Related Commands
- type — show how a name is interpreted (builtin, function, alias, file)
- which — locate executables (external; can differ from shell resolution)
- hash — manage the shell’s command lookup cache
- whereis — locate binaries/source/man pages (system database approach)