On Wed, Jun 18, 2025 at 04:57:01 +0300, nkkralev--- via Bug reports for the GNU 
Bourne Again SHell wrote:
> to occur.]#test1
> /bin/bash -c 'set -e ; /bin/echo $(ls /doesnt_exist) ; echo print1'
> #and the stdout/stderr displayed is:
> ls: cannot access '/doesnt_exist': No such file or directoryprint1

set -e is NOT an error catching mechanism.  The fact that a command
substitution used as an argument to an outer command had a nonzero
exit status is not relevant to set -e.

Only the outer command's exit status matters.

hobbit:~$ bash -e -c 'true $(false); echo survived'
survived
hobbit:~$ bash -e -c 'false $(false); echo survived'
hobbit:~$ bash -e -c 'false $(true); echo survived'
hobbit:~$ 

set -e may have originally been conceived as an error catching mechanism,
but the implementation does not conform to that idea, never has, and
never will.  Now, nearly five decades later, set -e is just a historic
artifact, and its behavior is specified by a committee to try to provide
backward compatibility for scripts that use it.

It is a TERRIBLE idea to use it in any new scripts.  You should perform
your own error checking instead.  Yes, by hand.  There is no other way
that actually works.

If you don't like that, then use a better programming language.

See also <https://mywiki.wooledge.org/BashFAQ/105>.

  • set -e doesn't ... nkkralev--- via Bug reports for the GNU Bourne Again SHell
    • Re: set -e... Greg Wooledge
      • Re: se... Steffen Nurpmeso
        • Re... microsuxx
          • ... nkkralev--- via Bug reports for the GNU Bourne Again SHell
            • ... microsuxx
              • ... microsuxx
                • ... nkkralev--- via Bug reports for the GNU Bourne Again SHell
                • ... Lawrence Velázquez
              • ... Robert Elz
    • Re: set -e... Chet Ramey

Reply via email to