A script that does `first=$(sort -u huge.txt | head -n 1)` exits with status 141 under `pipefail`, but only on large inputs.
AdvancedTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → 141 is 128 + 13, SIGPIPE. head exits after one line and closes the pipe; the writer is still producing output, gets SIGPIPE on its next write and dies. With small inputs the writer finishes before head exits, so there is no error, which makes it intermittent. pipefail turns the writer's 141 into the pipeline's status.
Diagnose →
set -o pipefail; yes | head -n 1; echo "rc=$? stages=${PIPESTATUS[*]}" # rc=141 stages=141 0
Fix → Avoid early-exit readers on a writer whose status you care about, or accept 141 explicitly:
first=$(sort -u huge.txt | sed -n '1p') # reads everything, no SIGPIPE
sort -u huge.txt > "$tmp"; first=$(head -n 1 "$tmp") # no pipe at all
# or tolerate exactly 141:
first=$(sort -u huge.txt | head -n 1) || [[ $? -eq 141 ]]
Prevent → Know that head, grep -q, grep -m1 and awk ... exit close pipes early. Under pipefail, either let the reader consume everything or handle 141.
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.IntermediateYou 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