Set -e behavior with pipelines: exit decision unclear
0 reputation · 18 Jul 2022, 00:40 UTC
Goal
We want a Bash script that terminates immediately when any component of a pipeline fails, without relying on pipefail.
Current Constraints
Using Bash 5.2, set -e is enabled. In a pipeline such as cmd1 | cmd2 | cmd3, only the exit status of cmd3 is normally considered. If cmd2 fails but cmd3 succeeds, the script continues.
Uncertainty
Documentation notes that behavior with pipelines is implementation‑specific. Some shells treat any failure in a pipeline as a trigger for set -e, while others do not. Subshells introduced by parentheses inherit set -e, but whether a subshell exit propagates to the parent is unclear.
Questions
- In Bash 5.2, does
set -ecause the script to exit if any command in a pipeline fails whenpipefailis not enabled? - When a subshell containing a pipeline fails under
set -e, does the parent shell exit or only the subshell? - How do these behaviors differ between Bash 4.x and Bash 5.x?