[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] errexit and local var=$(cmd) broken?
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] errexit and local var=$(cmd) broken? |
Date: |
Tue, 11 Aug 2015 08:56:53 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, Aug 11, 2015 at 12:46:51PM +0200, Christoph Mathys wrote:
> I was surprised when I found that the following two statements behave
> differently when run with set -o errexit.
http://mywiki.wooledge.org/BashFAQ/105
Start with that.
> # shell does not abort, even though errexit is set. Why not?
> local var=$(false)
The "local" command has its own exit status, which hides the exit
status of the command substitution.
> # shell does exit as I expect
> local var
> var=$(false)
Oh. You already know how to do it. Good, then I won't bother writing
out how to do it.
When you read the FAQ page above and see all the other ways that set -e
is fundamentally broken, you may decide to stop using it, at which point
all of these other issues just go away.
However, that masking of command substitution errors by "local" can be
an issue in other contexts besides just set -e. E.g. even if you are
explicitly checking for failures, this is wrong:
WRONG() {
local foo=$(try a thing) || return
}
Do it this way instead:
RIGHT() {
local foo
foo=$(try a thing) || return
}