Re: set -e doesn't trigger when command substitutions returns, why?
From:
microsuxx
Subject:
Re: set -e doesn't trigger when command substitutions returns, why?
Date:
Wed, 18 Jun 2025 20:45:19 +0200
u can try
trap '(( $? )) && exit 2' debug
On Wed, Jun 18, 2025, 5:05 PM Steffen Nurpmeso <steffen@sdaoden.eu> wrote:
> Greg Wooledge wrote in
> <20250618140055.GF28192@wooledge.org>:
> |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.
>
> ..this paradigm has however not spread widely, just yesterday flew
> by (still in tmux history):
>
> commit 611c1457ff32cbb39263139b599eb9b6058709fb
> Author: Ed Maste <emaste@FreeBSD.org>
> AuthorDate: 2025-06-05 18:50:03 +0000
> Commit: Ed Maste <emaste@FreeBSD.org>
> CommitDate: 2025-06-17 17:40:33 +0000
>
> release: Add set -e to abort upon failure
>
> We don't want to blindly continue and produce broken VM images if
> something goes wrong.
>
> Reviewed by: cperciva
> Event: Kitchener-Waterloo Hackathon 202506
> Sponsored by: The FreeBSD Foundation
> Differential Revision: https://reviews.freebsd.org/D50711
> ---
> release/scripts/mk-vmimage.sh | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/release/scripts/mk-vmimage.sh
> b/release/scripts/mk-vmimage.sh
> index 3ca369b04d8e..d9197de66695 100755
> --- a/release/scripts/mk-vmimage.sh
> +++ b/release/scripts/mk-vmimage.sh
> @@ -29,6 +29,7 @@
> # mk-vmimage.sh: Create virtual machine disk images in various formats.
> #
> #
> +set -e
>
> |If you don't like that, then use a better programming language.
> |
> |See also <https://mywiki.wooledge.org/BashFAQ/105>.
> --End of <20250618140055.GF28192@wooledge.org>
>
> --steffen
> |
> |Der Kragenbaer, The moon bear,
> |der holt sich munter he cheerfully and one by one
> |einen nach dem anderen runter wa.ks himself off
> |(By Robert Gernhardt)
>
>