Re: local keyword hides return code of command substitution
From:
Stephane Chazelas
Subject:
Re: local keyword hides return code of command substitution
Date:
Tue, 22 Sep 2015 17:22:15 +0100
User-agent:
Mutt/1.5.21 (2010-09-15)
2015-09-22 11:45:20 -0400, Greg Wooledge:
> On Tue, Sep 22, 2015 at 10:19:56AM -0400, idallen@home.idallen.ca wrote:
> > Description:
> > Adding a "local" keyword to a variable assignment hides the
> > return code of a command substitution. Same problem in both
> > bash and dash shells.
>
> Yes, this is how it works. If you care about the return value of a
> command used to initialize a local variable, you have to write it in
> two steps:
>
> foo() {
> local foo
> foo=$(bar) || return
> }
[...]
It also avoids problems with some other shells that support
"local" as a normal command when one forgets to quote the
$(command-subtitution) (where for instance
local foo=$(echo bar baz) is like local foo=bar baz)
Note that for "readonly", the assignment needs to be done first:
foo=$(bar) || return
readonly foo
--
Stephane