Great write-up, especially the part about the limits of `set -e` — that's the kind of thing you only really learn the hard way. Just wanted to add a detail that's helped a lot on my team: there's an option called `shopt -s inherit_errexit` that solves exactly the command-substitution case you mentioned. Without it, an error inside `$(...)` can slip through even with `set -e` active, because the subshell doesn't inherit the stop-on-error behavior. With the shopt on, that changes.
```bash
set -euo pipefail
shopt -s inherit_errexit
```
Worth turning on right after `set -euo pipefail`, especially in scripts that do a lot of assignment via `$(command)`. It doesn't fix everything (the `if` case you mentioned is by design, not a bug), but it closes one more common gap.
One more thing on `trap`: besides `EXIT`, it's worth catching `INT` and `TERM` separately when the script receives signals from an orchestrator (systemd, supervisord, k8s), because the default propagation behavior can vary depending on how the process was started. I've seen a script clean up fine on a manual Ctrl+C and leave junk behind when the container got killed with `SIGTERM`.