Skip to content
Latchkey

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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →