Bash set -e and subshells: error propagation during system upgrades
0 reputation · 14 Jun 2021, 04:32 UTC
Goal
Develop an upgrade script that aborts immediately when any command fails, preventing a half‑upgraded system.
Constraints
The script must rely on Bash’s documented error‑handling options: set -e (errexit), set -o pipefail for pipelines, and set -o errtrace to propagate traps into subshells. It should also use trap on EXIT for cleanup.
Unresolved behavior
While POSIX defines set -e for simple commands, its effect on commands executed inside subshells invoked by functions or command substitutions is unspecified. Additionally, the interaction between trap handlers and subshell failures is unclear: does an EXIT trap run if a subshell exits with a non‑zero status before the parent script terminates?
Questions
- Does
set -eautomatically propagate to subshells spawned by functions or command substitutions in Bash 5.1? - When a subshell fails, is the parent’s EXIT trap guaranteed to execute before the script exits?
- What minimal combination of
setoptions ensures consistent error propagation and cleanup across Bash 4.x and 5.x?