The error log says `ERROR: curl failed with exit code 0`, which is useless. The code is `if ! curl -fsS "$URL" -o "$out"; then log ERROR "curl failed with exit code $?"; fi`.
IntermediateTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → $? inside the then branch holds the status of the whole condition, ! curl ..., which is 0 (the negation succeeded). The original curl status is gone.
Diagnose →
bash -c 'if ! false; then echo "rc=$?"; fi' # rc=0
Fix → Capture the status without negation:
if curl -fsS --max-time 10 "$URL" -o "$out"; then
:
else
rc=$?
log ERROR "curl failed with exit code $rc" # 6 DNS, 7 connect, 22 HTTP >= 400, 28 timeout
fi
# or: curl ... || { rc=$?; log ERROR "curl failed ($rc)"; return "$rc"; }
Prevent → Save $? into a variable on the very next statement, before any [[ ]], echo or ! touches it.
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