Tested for Bash 5.0.2
According to the GNU Bash Reference Manual,
Bash performs the expansion [of a command substitution] by executing [the] command in a subshell environment
According to The Open Group Base Specifications Issue 6:
when a subshell is entered, traps that are not being ignored are set to the default actions.
So when running the following script:
function a {
trap -p EXIT
}
trap "echo 'parent'" EXIT
echo "$(a)"
(a)
trap - EXIT
echo 'exiting'
... i would expect an output of:
exiting
... but instead I get:
trap -- 'echo '\''parent'\''' EXIT
trap -- 'echo '\''parent'\''' EXIT
exiting
... meaning that the function a
- eventhough it is being run in a subshell - is seeing the the parent shell's trap commands (via trap -p
) but not executing them.
What is going on here?