I'm not really clear on which traps are inherited when in bash
. The manpage says:
When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment [...] [T]raps caught by the shell are reset to the values inherited from the shell's parent, and traps ignored by the shell are ignored[.]
Later on it says that subshells behave the same way.
This makes enough sense to me, I think. It sounds like if a shell unsets a trap, that gets remembered by any subshells and whatnot, while if a shell sets a trap, that gets forgotten. I don't get why that design choice was made, but I at least think I understand what's going on.
But what about commands that are builtins or shell functions? And do the two ever behave differently (with respect to trap inheritance)? I can't find a comprehensive description in the manual--as far as I can tell, anything goes. It seems like they are usually inherited from the parent, but there are exceptions like ERR
and RETURN
, each of which are only inherited by functions when certain shell options are being used.
My questions are:
What exactly is the typical way trap inheritance works for builtins and functions? E.g., are there any subtleties regarding setting vs. unsetting traps, the way there appears to be with most commands?
Do all functions and builtins behave the same way? (Please don't tell me each builtin has a separate set of rules...)
What are the exceptions? I.e., what signals behave differently by default, and what functions can have their default behavior changed by shell options and whatnot? I know about
ERR
andRETURN
, but are there any others? Try as I might, I couldn't find a nice simple list of this anywhere.When, if ever, can a function or builtin affect the traps of a parent? How does
trap - SIGSPEC
vs.trap '' SIGSPEC
play into this?
Thanks!
PS: I am using GNU bash version "4.4.19(1)-release".