`notify.sh: line 9: SLACK_WEBHOOK: unbound variable` — but the webhook is meant to be optional.
BeginnerTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → set -u treats any reference to an unset variable as fatal, including in [[ -n $SLACK_WEBHOOK ]]. The variable is optional, so it is legitimately unset on hosts without Slack.
Diagnose →
sed -n '9p' notify.sh
env | grep -c SLACK_WEBHOOK # 0 = not set in this environment
Fix → Expand with an explicit default wherever the variable is optional, and fail with a clear message where it is required:
SLACK_WEBHOOK=${SLACK_WEBHOOK:-} # optional: default to empty, once, at the top
: "${S3_BUCKET:?S3_BUCKET must be set in /etc/opskit/opskit.conf}" # required: abort with a message
if [[ -n $SLACK_WEBHOOK ]]; then send_slack "$msg"; fi
Prevent → Declare every config variable at the top with :- or :?, so the contract is visible in one place. Do not "fix" it by removing set -u; it catches real typos like $S3_BUKET.
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.IntermediateOn 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.AdvancedYou added `trap 'echo "failed at line $LINENO" >&2' ERR`, but when a command inside a function fails, nothing is printed and the script just exits.Intermediate