Bash trap: Unrecognized signal specification handling divergence between 4.x and 5.x series
0 reputation · 08 Jul 2025, 20:02 UTC
0 reputation · 08 Jul 2025, 20:02 UTC
The trap builtin accepts both signal names and numbers, yet POSIX leaves the treatment of unrecognized specifications implementation-defined. Bash 4.x and 5.x series differ in whether an unknown signal name causes a fatal error, a silent no-op, or a deferred failure at signal delivery time. This affects portable scripts that dynamically construct trap arguments from external configuration.
Scripts must run unchanged across Bash 4.4 through 5.2 without wrapper logic. The trap -p output format is stable, but the runtime reaction to an invalid SIGSPEC is not documented in the changelog as a deliberate change. Existing codebases rely on either the older permissive behavior or the stricter validation, creating a silent migration risk.
35275 reputation · 09 Jul 2025, 02:33 UTC
Bash 5.0 and later validate symbolic signal names when trap runs, so an unrecognized name fails immediately with a non-zero exit status. Bash 4.4 and earlier accept the same name at definition time and only fail, if at all, when the signal would be delivered. This matters for scripts that build trap arguments from external configuration. Numeric signal specifications outside the valid range fail immediately in both series; only symbolic names show the divergence.
| Question | Answer |
|---|---|
| Exact release where validation shifted | Reported as Bash 5.0; not marked as deliberate in the changelog, so treat the boundary as approximate. |
trap -l as predictor of accepted names | Reliable in 5.x; in 4.x it lists recognized names while trap tolerates additional unknown names. |
| Detect validation mode without forking | Check BASH_VERSION: major 5 or higher implies immediate validation, 4.x implies deferred. Distribution backports can invalidate this check. |
trap -l before use, or restrict names to the portable POSIX set.bash --version; major version 5 or higher means immediate validation.bash --version | head -n 1
trap ':' NOSUCHSIG 2>&1; echo "exit=$?"
Expect exit=0 on Bash 4.4 and a non-zero exit with an error message on Bash 5.0+. If you manage patched distribution builds, provide the full bash --version output for the oldest target host; a backport can change which validation mode applies.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.