Check Command Success in Bash

Branch on exit codes instead of parsing output.

macOSbashautomationshellscriptingLow risk
Use if command; then ...; fi or command || fallback to react to success and failure.

01Command

Command
$ if ! grep -q "error" app.log; then echo "no errors"; fi

02What it does

Every command returns an exit status, and shell conditionals test it directly, so scripts can make decisions without text munging. Using grep -q to test without output shows the pattern: quiet, fast and scriptable.

03Risk

RiskLow risk
RequirementsmacOS default zsh/bash (bash 3.2+; some features need bash 4+ via Homebrew)
CompatibilitymacOS with bash 3.2+ (GNU bash 4+ via Homebrew for advanced features)
!Warning: set -e and if interact subtly; a failing command in a condition does not exit under errexit.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Developer