bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Command substitution and errexit


From: DJ Mills
Subject: Re: Command substitution and errexit
Date: Mon, 15 Oct 2012 15:44:22 -0400

On Mon, Oct 15, 2012 at 11:23 AM, Nikolai Kondrashov
<address@hidden> wrote:
> Hi everyone,
>
> I've noticed that errexit is disabled inside command substitution.
> Is this intentional?
>
> It makes it hard to save and restore errexit state. I.e. you can't simply
> say
> opts=`set +o`, because errexit will always be stored as off. What's
> interesting, $SHELLOPTS shows it still on inside the command substitution.
>
> Sincerely,
> Nick
>

Errexit only causes the shell to exit when the exit status of the
entire simple command is false.
Therefore, it depends on the context. For example:
set -e
foo=$(false)

This will exit. Assignments preserve the exit status of the command
within a command substitution. However,
set -e
true "$(false)"

This will not exit. The exit status is not preserved in that case, and
the exit status of the entire command is true.

My advice is to stop using errexit at all. Your life will be much,
much easier that way.

As an aside, check out http://mywiki.wooledge.org/BashFAQ/105 for
other issues with errexit.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]