2

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!

jmrah
  • 5,715
  • 3
  • 30
  • 37
  • 1
    It's strange. I would always expect two messages, because `ls` delivers an exit status that is different from zero and therefore also the exit status of the subshell. – Cyrus Jun 28 '20 at 21:21
  • 1
    I believe that `bash` optimizes the subshell away when there is only one (non-shell) command in the subshell. – chepner Jun 28 '20 at 23:07
  • @chepner I googled your suspicion, and I think believe you're right. There seems to be certain scenarios where bash 'optimizes-out' creating a subshell. I've updated my question with this info. – jmrah Jun 29 '20 at 02:50
  • 1
    You've already got yourself an answer,what are you asking now? – oguz ismail Jun 29 '20 at 04:05
  • @oguzismail, I've edited my question, and I've added an answer below. – jmrah Jun 29 '20 at 10:45
  • The *question* isn't a duplicate though, even though the underlying root cause to the question is answered in that link,and others. – jmrah Jun 29 '20 at 10:55
  • 1
    The guidance on Stack Overflow is actually "if the _answer_ is the same, the questions can be regarded as duplicates". This is obscure enough that I'm still thinking about whether to close as a dupe. The fact that your answer defers to the nominated question is a strong hint that this _should_ in fact be closed as a dupe. – tripleee Jun 29 '20 at 11:00
  • @tripleee thanks for pointing out that definition of a duplicate. I was wondering about that. In that case, I'm on-board with closing this as a duplicate. But how do you list the duplicate question/answer that is on unix.stackexchange.com? – jmrah Jun 29 '20 at 11:27
  • We can't link to cross-site duplicates in any official way, just link them informally like you already have. Closing this as a duplicate will not remove anything, as such (though closed questions can get deleted if they receive very little traffic or if they are downvoted to a net negative score, which I don't think will happen to this one). – tripleee Jun 29 '20 at 11:55
  • For the record, this can be reopened if three regular users with reopen voting privileges request reopening, or if myself or another gold badge owner (or a mod) decides to reopen it. – tripleee Jun 29 '20 at 11:56

1 Answers1

0

I'm answering my own question for closure.

The reason why this is happening is because Bash will not create a subshell in certain scenarios, in an attempt to optimize. One such scenario where Bash will not create a subshell is in the OPs case, where the subshell contains a single simple command.

Here is a StackExchange question who's answers give more details about this Bash behaviour.

As for documentation, I'm going to repeat what's in the Stackexchange answer. The only documentation I can find about this behaviour is in the Bash source file here

jmrah
  • 5,715
  • 3
  • 30
  • 37