A pipeline's exit status hides the failure
The exit status of a pipeline is the exit status of its last command, so piping a command into anything at all discards whether it worked. set -e does not help. The file that lands on disk afterwards is non-empty, one line long, and wrong.
The symptom
Alone, a failing command reports failure:
$ bash -c 'false; echo "exit=$?"'
exit=1
Pipe it into anything — even something that does nothing — and the failure is gone:
$ bash -c 'false | cat; echo "exit=$?"'
exit=0
$ bash -c 'false | tee out.log >/dev/null; echo "exit=$?"'
exit=0
$? after a pipeline is the exit status of the last command in it. cat succeeded. tee succeeded. Whether anything upstream produced data is not part of the answer.
set -e does not cover this
This is the part that gets people, because set -e is the thing you add precisely so that failures stop the script:
$ bash -c 'set -e; false | cat; echo "set -e did NOT abort, exit=$?"'
set -e did NOT abort, exit=0
The script carried on. And the control, so this cannot be dismissed as set -e being broken or unsupported in this shell:
$ bash -c 'set -e; false; echo "this line should never print"'
$ echo $?
1
Nothing printed, bash exited 1. set -e works exactly as advertised — it aborts on a command that fails, and a pipeline whose last command succeeded did not fail.
What it looks like in a real script
The pattern this actually breaks is "fetch a list, then act on it". Here the fetch is aimed at a repository that does not exist, and the result is teed to a file the way a real script would:
FAILING (repo does not exist):
pipeline $? = 0
PIPESTATUS = 1 0
file exists = yes
file bytes = 118
grep -c '' lines = 1
CONTROL (repo exists):
pipeline $? = 0
PIPESTATUS = 0 0
file exists = yes
file bytes = 12
grep -c '' lines = 1
Read the two blocks against each other. $? is 0 in both. The file exists in both. It is non-empty in both. It contains exactly one line in both. Every check a careful script would make returns the same answer for the failure as for the success.
The only line that differs is PIPESTATUS = 1 0 — the first command exited 1, the second exited 0 — and $? reports only the second.
What is in the 118 bytes:
$ xxd a.txt
00000000: 7b22 6d65 7373 6167 6522 3a22 4e6f 7420 {"message":"Not
00000010: 466f 756e 6422 2c22 646f 6375 6d65 6e74 Found","document
00000020: 6174 696f 6e5f 7572 6c22 3a22 6874 7470 ation_url":"http
00000030: 733a 2f2f 646f 6373 2e67 6974 6875 622e s://docs.github.
00000040: 636f 6d2f 7265 7374 2f72 6570 6f73 2f72 com/rest/repos/r
00000050: 6570 6f73 2367 6574 2d61 2d72 6570 6f73 epos#get-a-repos
00000060: 6974 6f72 7922 2c22 7374 6174 7573 223a itory","status":
00000070: 2234 3034 227d "404"}
An error object, written into the data file, counted as one record.
A downstream step that reads this file does not see an error. It sees one item. "Nothing to do" and "the fetch failed" are now the same observation, and the second one is indistinguishable from an empty day's work.
The producer's error body goes to stdout
Worth knowing on its own, because it explains how the error ended up in the data file rather than the log. Splitting the two streams of the failing command:
$ gh api repos/octocat/this-repo-does-not-exist-xyz1234 --jq '.name' 2>/dev/null
{"message":"Not Found","documentation_url":"https://docs.github.com/rest/repos/repos#get-a-repository","status":"404"}
$ echo $?
1
$ gh api repos/octocat/this-repo-does-not-exist-xyz1234 --jq '.name' 2>&1 >/dev/null
gh: Not Found (HTTP 404)
$ echo $?
1
Two different messages on two different streams. The human-readable one goes to stderr, which a script usually discards; the raw error JSON goes to stdout, which is the stream being captured. Note also that --jq '.name' did not apply — the filter is bypassed on an error response, so the consumer that expected one field receives a whole object.
And it arrives with no trailing newline, which is its own small problem: the } runs straight into the next thing printed. That one is a separate note.
The fixes, measured
set -o pipefail
Makes the pipeline's status the rightmost non-zero one:
$ bash -c 'set -o pipefail; false | cat; echo "exit=$?"'
exit=1
Combined with set -e, the script stops where you expected it to:
$ bash -c 'set -eo pipefail; false | cat; echo "never printed"'
$ echo $?
1
Nothing printed, exit 1. This is the one-line fix for most scripts, and set -euo pipefail at the top of a bash script is worth the habit.
I expected to have to add a portability caveat here — "pipefail is a bash extension, use PIPESTATUS in #!/bin/sh scripts" — and the measurement says the opposite. dash is installed on this machine, and it has pipefail:
$ dash -c 'set -o pipefail; false | cat; echo "with pipefail exit=$?"'
with pipefail exit=1
$ dash -c 'false | cat; echo "without it exit=$?"'
without it exit=0
Two controls, because "an unrecognised option that silently did nothing" would produce a misleading exit=1 if false | cat happened to fail for another reason. First, dash does validate -o names, so pipefail is genuinely recognised rather than ignored:
$ dash -c 'set -o nosuchoption_xyz; echo "continued"'
dash: 1: set: Illegal option -o nosuchoption_xyz
$ echo $?
2
Second, it does not simply fail everything:
$ dash -c 'set -o pipefail; true | cat; echo "pipefail+success exit=$?"'
pipefail+success exit=0
pipefail was standardised in POSIX.1-2024, and this dash has it. So the portability advice runs the other way round from the way I was going to write it — see the next section.
PIPESTATUS, when you need to know which stage failed
PIPESTATUS = 1 0
One element per stage, left to right. It must be read immediately — the very next command overwrites it, including an echo. The first version of the measurement above read it one line too late and reported a single 0, which is how long that mistake survives if you do not check it against a case you know should fail.
This is the one that is not portable. PIPESTATUS is a bash array, and dash does not have it — or arrays at all:
$ dash -c 'false | cat; echo "PIPESTATUS=[${PIPESTATUS[*]}]"'
dash: 1: Bad substitution
So of the two mechanisms, the one I assumed was bash-only works in both shells here, and the one I was about to recommend as the portable fallback is the bash-only one. If you want a single rule that holds in both: use pipefail.
Or do not use a pipeline
Two statements, each with its own exit status, is often clearer than a pipeline plus the machinery to recover what the pipeline threw away:
gh api "repos/$repo" --jq '.name' > names.txt # $? is gh's, and only gh's
wc -l < names.txt
Check the thing you actually care about
The deepest version of the fix is not about exit codes at all: a step that needs *n* records should assert that it got *n* records. file exists and file non-empty both passed on the failure above. "Parses as the shape I expect" would not have.
Why this one is worth a page
- **Adding a pipe to a working command silently removes its error
reporting.** cmd and cmd | tee log differ in observability, not just in output.
- The flag people add for safety does not cover it.
set -eis widely
understood as "stop on error" and does not stop on this one.
- The failure is shaped like success. Non-empty file, one line, exit 0.
- The diagnostic field expires instantly.
PIPESTATUSis correct for
exactly one command, so the natural way to inspect it destroys it.
The general form: a composite's status is not its components' statuses. When you join two things together, decide explicitly what the result means when one half fails, because the shell has already decided for you.
Measured on
Windows 11, Git Bash MSYSTEM=MINGW64, GNU bash as /bin/bash, GitHub CLI 2.92.0, 2026-11-28. The gh requests are two calls to api.github.com, a public documented API, well inside its unauthenticated rate limit: one repository that does not exist and octocat/Spoon-Knife, which does. The failing and control runs are the same script with only the repository name changed. The dash measurements use the dash shipped with Git for Windows (binary dated 12 Feb 2026); /bin/sh on this machine is bash 5.2.37, so sh -o pipefail also works here, which is exactly why the shebang question has to be settled by testing the shell you will actually run under rather than the one called sh locally. Only the dash installed here was tested.
Not measured, but worth knowing before you rely on this: pipefail entered the standard through Austin Group Defect 789, and implementations caught up at different times — dash added it in 5.13, so builds older than that still reject it outright. If a script has to survive a shell you cannot inspect, ask for the option before depending on it:
(set -o pipefail 2>/dev/null) && set -o pipefail
That line is cited from documentation, not measured here. The measurements above stand without it.