[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: set -e doesn't trigger when command substitutions returns, why?
From: |
Greg Wooledge |
Subject: |
Re: set -e doesn't trigger when command substitutions returns, why? |
Date: |
Wed, 18 Jun 2025 10:00:55 -0400 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
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 trigger when command substitutions returns, why?, Никола Константинов Кралев, 2025/06/18
- Re: set -e doesn't trigger when command substitutions returns, why?,
Greg Wooledge <=
- Re: set -e doesn't trigger when command substitutions returns, why?, Steffen Nurpmeso, 2025/06/18
- Re: set -e doesn't trigger when command substitutions returns, why?, microsuxx, 2025/06/18
- Re: set -e doesn't trigger when command substitutions returns, why?, Никола Константинов Кралев, 2025/06/18
- Re: set -e doesn't trigger when command substitutions returns, why?, microsuxx, 2025/06/19
- Re: set -e doesn't trigger when command substitutions returns, why?, microsuxx, 2025/06/19
- Re: set -e doesn't trigger when command substitutions returns, why?, Никола Константинов Кралев, 2025/06/23
- Re: set -e doesn't trigger when command substitutions returns, why?, Lawrence Velázquez, 2025/06/24
- Re: set -e doesn't trigger when command substitutions returns, why?, Robert Elz, 2025/06/19
Re: set -e doesn't trigger when command substitutions returns, why?, Chet Ramey, 2025/06/18