Bash

Check Command Exists Before Using It

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`command -v` is the portable, fast way to check whether a binary is on $PATH. Avoid `which` (its behavior varies between systems).
Bash
Raw
need() {
    command -v "$1" >/dev/null 2>&1 || {
        echo "Required command not found: $1" >&2
        exit 1
    }
}

# Use at top of script — fail fast if deps are missing
need jq
need curl
need rsync

# Or branch on optional deps
if command -v fd >/dev/null; then
    fd --type f
else
    find . -type f
fi
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.