`backup` calls `upload_to_s3`, which fails, yet `if backup; then echo "Backup OK"; fi` prints "Backup OK". `set -e` is on.
AdvancedTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → set -e is suspended for everything executed as part of an if/while condition, the left side of &&/||, or after !, including the entire body of any function called there. Inside backup, the failed upload_to_s3 is ignored, the next command succeeds, and the function returns 0.
Diagnose →
bash -c 'set -e; f(){ false; echo "kept going"; }; if f; then echo "reported OK"; fi'
Fix → Make functions return failure explicitly instead of relying on set -e inside them:
backup() {
create_archive || return 1
upload_to_s3 "$arch" || return 1
verify_upload "$arch" || return 1
}
if backup; then log INFO "Backup OK"; else alert CRITICAL "backup failed"; fi
Or call it unconditionally (backup) and handle the outcome in the ERR/EXIT trap.
Prevent → Treat set -e as a safety net, not as error handling. Any function used in a condition must check its own steps.
More on Troubleshooting: strict mode, exit codes, pipelines and traps
A script with `set -e` stops silently right after `((retries++))` on its first pass. No error message, exit code 1.Intermediate`notify.sh: line 9: SLACK_WEBHOOK: unbound variable` — but the webhook is meant to be optional.BeginnerOn a CentOS 7 / Amazon Linux 2 host, `deploy.sh "${extra_args[@]}"` fails with `extra_args[@]: unbound variable`. It works on your laptop.Advanced`local release=$(curl -fsS "$API/latest")` fails with a 503, yet the function happily continues and deploys an empty release name.IntermediateA check `procs=$(ps aux | grep -v grep | grep -c '[n]ginx: worker')` kills the script under `set -euo pipefail` whenever nginx is down, before the alert code runs.IntermediateA script that does `first=$(sort -u huge.txt | head -n 1)` exits with status 141 under `pipefail`, but only on large inputs.Advanced