Bash ERR trap scope for failure-only alerts in non-interactive shells
0 reputation · 30 Dec 2022, 13:42 UTC
Bash ERR trap scope for failure-only alerts in non-interactive shells
Goal: a Bash script should emit exactly one alert when a simple command fails unexpectedly, while remaining silent for failures used as conditions in if, while, until, after !, or in &&/|| lists. The script may run under cron or non-interactive SSH where a D-Bus session bus and notification daemon are unavailable.
Uncertainty: the ERR exemption rules and errtrace inheritance for command substitutions, subshells, and functions are Bash-version-specific. The trap must also choose a headless fallback such as logger or mail when notify-send cannot reach a bus, without changing the script's exit status or blocking on a GUI call.
Which Bash version reliably enforces the current ERR exemption rules? Should bus detection happen once at startup or per failure? How can the trap preserve the original exit status when using a non-GUI fallback?