This is from the manual for bash set options (for set -E)
-EIf set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not inherited in such cases.
What does "trap on ERR" mean? what does "trap on ERR is inherited by shell functions in a subshell environment"? Can a shell script proceed even after the trap and the trap is passed to a subshell? Can someone elaborate on this with a simple example, please?
The Bash manual on
trapsays:The material you quote says that
trap '…' ERRis not normally inherited by a shell function, a command substitution or commands executed in a subshell environment. However, if you useset -E, then theERRtrap is inherited.The sentence means:
set -Eis set, then any trap on ERR is inherited by shell functions.set -Eis set, then any trap on ERR is inherited by command substitutions.set -Eis set, then any trap on ERR is inherited by commands executed in a subshell environment.The 'in a subshell environment' clause does not apply to shell functions or command substitutions.
I can't parse your question "Can a shell script proceed even after the trap and the trap is passed to a subshell?" A shell script can proceed after a trap — what happens depends on the commands in the
argpart of thetrapcommand.Here's a shell script that demonstrates
set -Ein action:When run, it produces:
As you can see, the
ERRtrap fires on the failinglscommand run on its own, but doesn't fail when run in a function or in a subshell or as part of (some) command substitutions.When the
set -Eoption is set, theERRtrap fires when the command failed in the function, in the subshell, and in the command substitution.Curiously, the
ERRtrap does fire in a command substitution likex=$(ls /non-existent)but not in the more complex example shown above. I'm not clear whether that's supposed to happen. I'm usingGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)— it's possible that later versions of Bash (4.x) do not behave the same.