Exit Codes & Error Handling
SH · Bash Scriptingsyntax
$?
set -e
set -o pipefail
command || handle_errorexample
#!/usr/bin/env bash
set -euo pipefail
grep -q 'ready' status.txt || { echo 'Not ready'; exit 1; }
if ! deploy_app; then
echo "Deploy failed with code $?"
rollback
exit 1
fiNote $? holds the exit code of the last command (0 = success, non-zero = failure). set -e aborts the script on any non-zero exit. set -u treats unset variables as errors. set -o pipefail catches failures in piped commands. Always use all three in production scripts.