[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Command substitution and errexit
From: |
Nikolai Kondrashov |
Subject: |
Re: Command substitution and errexit |
Date: |
Mon, 15 Oct 2012 23:47:06 +0300 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:10.0.7) Gecko/20120922 Icedove/10.0.7 |
Hi Daniel,
On 10/15/2012 10:44 PM, DJ Mills wrote:
On Mon, Oct 15, 2012 at 11:23 AM, Nikolai Kondrashov
<Nikolai.Kondrashov@redhat.com> wrote:
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.
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.
Thank you for a convincing explanation. However, I'll try to stick with
errexit. I've written noticeable amount of code with it before and had
to adjust it to bash 4 later. So far it looks OK to me, but I'll try to be
more critical of this solution from now on, thanks to your advice.
I'm trying to do a test framework, with expectations of noticeable amount of
setup code and little actual tests and I'd like to have restriction on setup
code correctness without explicit error checking to make it as easy to write
as possible.
As an aside, check out http://mywiki.wooledge.org/BashFAQ/105 for
other issues with errexit.
I've stumbled on this page today, but didn't read it. Now I solved the first
exercise correctly, but failed on the second. I don't use "let" and "(( ))"
"command", though.
Still, all of the above doesn't answer my original question: have I found a
bug, or is command substitution supposed to disable errexit? It looks like a
bug, because documentation doesn't seem to mention it and because SHELLOPTS
still lists errexit as enabled in command substitution.
Thank you.
Sincerely,
Nick