Check Command Success in Bash
Branch on exit codes instead of parsing output.
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"; fi02What 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
!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
- bash.1.en.html manual pageEvidence: The bash manual page documents exit status.
Last verified: 2026-10-13 · Category: Developer