Consider this minimal example, which I run as an executable.
#!/bin/bash
set -E
trap 'echo "ERR trap triggered"' ERR
(
echo "hello" >/dev/null
ls /root/
)
Notice the two ERR trap messages in the following output:
ls: cannot open directory '/root/': Permission denied
ERR trap triggered
ERR trap triggered
However, when I make the subshell contain a single statement, such as:
#!/bin/bash
set -E
trap 'echo "ERR trap triggered"' ERR
(
ls /root/
)
I only get one ERR trap message:
ls: cannot open directory '/root/': Permission denied
ERR trap triggered
I was hoping someone could explain to me the reason behind the difference in output. Thanks!