Consider the simple `ssxtrap' script: | #!/bin/bash | | echo ext pid=$BASHPID | # trap 'echo "ext exit trap in $BASHPID"' EXIT | | (echo int pid=$BASHPID | trap 'echo "int exit trap in $BASHPID"' EXIT | sleep 99999) & | | wait | echo "subshell done"
Let's run it: Terminal A Terminal B ---------- ----------- | $ ./ssxtrap | ext pid=10370 | int pid=10371 | $ kill 10371 | int exit trap in 10371 | ./ssxtrap: line 10: 10371 Terminated ... | subshell done ... but if I uncomment the line that installs exit trap in the main shell, the output changes to: Terminal A Terminal B ---------- ----------- | $ ./ssxtrap | ext pid=10373 | int pid=10374 | $ kill 10374 | ./ssxtrap: line 10: 10374 Terminated ... | subshell done | ext exit trap in 10373 i.e. subshell exit trap is not invoked anymore. Since subshell exit trap shall not depend from main shell exit trap, this behaviour looks like a bug for me. Verified on: Solaris 11.3, bash 4.1.17(1)-release (i386-pc-solaris2.11) CentOS Linux 7.2.1511, bash 4.2.46(1)-release (x86_64-redhat-linux-gnu) CentOS Linux 7.2.1511, bash 5.0.0(1)-release (x86_64-pc-linux-gnu) Best regards, Konstantin Andreev