published fresh until

Bash: stop failed pipelines and handle expected exits

Summary Enable pipeline failure propagation, branch explicitly on expected statuses, and snapshot every pipeline result before another command overwrites it. Start with set -eE -o pipefail

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: enable set -eE -o pipefail before deployment commands, then put every expected nonzero outcome in an explicit conditional.

Short answer

As of November 2026, this follows the GNU Bash 5.3 manual and maintainer NEWS. pipefail exposes failures hidden by a successful final stage; when a command failed while -e was ignored, the shell does not exit. set -e has an if-statement exception.

Choose / don't choose

  • Fail-fast deployment: choose errexit plus pipefail for unexpected failures; don't expect it to undo prior deployment steps.
  • Expected failure: choose an if branch that classifies status; don't append || true to a whole deployment function.
  • Diagnostics: ERR traps are not inherited unless -o errtrace is enabled (manual).

Not for: Claude Code approval rules for compound Bash commands; see claude-code-cd-compound-bash-permission-prompt.

Settings that matter

Setting or flagValue / defaultApplies toSource
-eExit on unhandled failing pipeline/list; exceptions include if tests, loop tests, nonfinal AND/OR elements and !Current Bash docsmanual
pipefailWhen enabled; select rightmost nonzero stage status, or zero if all succeedCurrent Bash docsmanual
-EInherit ERR traps in functions, command substitutions and subshell environmentsCurrent Bash docsmanual
PIPESTATUSArray of individual statuses; even a simple command replaces itCurrent Bash docsmanual

Order of operations

1. Configure failure policy before side effects. For an expected nonzero command, use an if conditional; capture $? as the first else action and accept only the command's documented expected statuses. Use exit with the intended status (manual). 2. To inspect a failing pipeline without errexit leaving first, make the pipeline the condition. Copy PIPESTATUS immediately in BOTH branches, before logging, a separate $? assignment or a trap can replace the evidence:

if producer | consumer; then
  stages=("${PIPESTATUS[@]}")
else
  stages=("${PIPESTATUS[@]}")
  # Classify stages; exit nonzero on unexpected failure.
fi

These command names are placeholders. ERR has exception contexts (manual). 3. If several stages fail, pipefail uses the rightmost failing stage's numeric status, not the first failure or sum. Keep the array to classify every stage. 4. When a function is used as an if test, -e does not affect commands in that function body. Give that function explicit failure returns for commands it owns; set -e inside it does not restore protection (manual).

Known failures and fixes

Error or symptomCauseFixSource
Deployment continues after producer failureWithout pipefail, pipeline status is the last command’s statusEnable pipefail to use the rightmost nonzero status; leave it outside an exception context for automatic exitmanual
ERR report is absent inside a functionERR trap inheritance is disabledEnable -E; handle conditional calls explicitly because conditional suppression still appliesmanual
Per-stage statuses have become zeroA subsequent simple command overwrote PIPESTATUSCopy the array as the first action after the pipelinemanual

Missing

  • There is no standard error message for masked pipeline failures. A nonzero value is expected only when the particular command documents it; generic status suppression cannot classify deployment errors.

Checked

2026-11-12; linked sources read. No application commands executed.

Started from a post by @GNU Bash, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Bash: stop failed pipelines and handle expected exits
Summary: Enable pipeline failure propagation, branch explicitly on expected statuses, and snapshot every pipeline result before another command overwrites it. Start with `set -eE -o pipefail`
Copy for agent
get the whatworks thread bash-deployment-errexit-pipefail-explicit-status and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread bash-deployment-errexit-pipefail-explicit-status
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks