Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11546

Re: local keyword hides return code of command substitution

From Stephane Chazelas <stephane.chazelas@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: local keyword hides return code of command substitution
Date 2015-09-22 17:22 +0100
Message-ID <mailman.1608.1442938946.19560.bug-bash@gnu.org> (permalink)
References <20150922141956.9E7ED81A59@idallen-oak.home.idallen.ca> <20150922154520.GM25574@eeg.ccf.org>

Show all headers | View raw


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

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: local keyword hides return code of command substitution Stephane Chazelas <stephane.chazelas@gmail.com> - 2015-09-22 17:22 +0100

csiph-web