which Command Reference: Usage & CI Examples
which locates an executable in the current PATH.
which reports the full path of a command as the shell would resolve it. In CI it confirms a tool is on PATH before a step depends on it, though command -v is the more portable equivalent.
Common flags and usage
- which <cmd>: print the resolved path (exit 1 if absent)
- which -a <cmd>: show every match on PATH
- command -v <cmd>: POSIX-portable alternative
- type <cmd>: also reveals aliases and builtins
Example
shell
command -v psql >/dev/null || { echo "psql is required"; exit 1; }In CI
Prefer command -v over which in scripts: it is a shell builtin, always present, and avoids the missing-which-binary problem on slim images. Use it to fail fast with a clear message when a prerequisite tool is not installed.
Key takeaways
- which resolves a command to its PATH location.
- command -v is the portable, always-present alternative.
- Use it to fail fast when a required tool is missing.
Related guides
uname Command Reference: Flags, Usage & CI ExamplesReference for uname: -a, -s, -m, -r, the OS/arch detection use, and a CI example that branches a script on op…
getent Command Reference: Databases, Usage & CI ExamplesReference for getent: hosts, passwd, group, the NSS-aware lookup advantage, and a CI example that resolves a…
ulimit Command Reference: Flags, Usage & CI ExamplesReference for ulimit: -n, -u, -a, soft vs hard limits, the too-many-open-files fix, and a CI example that rai…