Check that required commands are installed
Fail early with a helpful message when a script depends on tools that are missing.
- Bash
- GreenSnippets
- MIT
require_commands() {
local missing=()
local cmd
for cmd in "$@"; do
command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd")
done
if (( ${#missing[@]} > 0 )); then
echo "Missing required commands: ${missing[*]}" >&2
return 1
fi
}Example / expected output
require_commands curl jq git || exit 1How it works
Scripts that die halfway because jq is missing leave things half-done. command -v is the portable, builtin way to test for a command (unlike which). Collect all missing tools and report them together so the user installs everything in one go.
This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.
Related snippets
- Bash
Retry a shell command with backoff
Re-run a flaky command a few times with increasing delays and a clear failure exit code.
- Bash
Parse command-line flags with getopts
Handle -v, -o FILE and -h in a bash script with getopts and a usage message.
- Bash
Run commands in parallel with xargs
Process many files concurrently with xargs -P, safely handling spaces and newlines in names.
- Bash
Timestamped logging functions for scripts
Small log, warn and die helpers that write ISO timestamps to stderr and keep stdout clean for data.

