bug-bash
[Top][All Lists]
Advanced

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

Re: Question about the return value of 'local'


From: Bob Proulx
Subject: Re: Question about the return value of 'local'
Date: Thu, 13 Dec 2012 01:44:21 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Francis Moreau wrote:
> I found that the return value of 'local' keyword is counter intuitive
> when the value of the assignment is an expression returning false. In
> that case the return value of local is still true. For example:
> 
>   local foo=$(echo bar; false)
> 
> returns true

Yes.  The creation of the local variable foo was successful.

> whereas:
> 
>   foo=$(echo bar; false)
> 
> returns false, that is removing the 'local' keyword has the opposite 
> behaviour.

The "local" function itself is either there or it isn't.  If it is
there then the return value is the return from local.  If it isn't
there then it isn't there and the return value is of whatever you are
checking.

If the local value is there then you may use it to assign multiple
values.  How does your thinking change when thinking about having
multiple values to local?

  local v1=true v2=false v3="green" v4=42

If the entire operation is successful then it returns 0.  If any of
the operands fail then it returns non-zero.

> The help of 'local' is rather obscure about the description on its return 
> value:
> 
>     Returns success unless an invalid option is supplied, an
>     error occurs, or the shell is not executing a function.
> 
> "an error occurs" is rather meaningless IMHO.
> 
> Could anybody explain me why 'local' returns true in this case ?

It returns 0 because the local variable was successfully created.

> Also I tried to find in the documentation, where the specification of
> the return value of an asignment is but have failed. Could anybody
> point me out the location ?

The bash manual contains this:

       local [option] [name[=value] ...]
              For each argument, a local variable named name is
              created, and assigned value.  The option can be any of
              the options accepted by declare.  When local is used
              within a function, it causes the variable name to have a
              visible scope restricted to that function and its
              children.  With no operands, local writes a list of
              local variables to the standard output.  It is an error
              to use local when not within a function.  The return
              status is 0 unless local is used outside a function, an
              invalid name is supplied, or name is a readonly
              variable.

See also 'export'.  Compare and contrast.

       export [-fn] [name[=word]] ...
       export -p
              The supplied names are marked for automatic export to
              the environment of subsequently executed commands.  If
              the -f option is given, the names refer to functions.
              If no names are given, or if the -p option is supplied,
              a list of all names that are exported in this shell is
              printed.  The -n option causes the export property to be
              removed from each name.  If a variable name is followed
              by =word, the value of the variable is set to word.
              export returns an exit status of 0 unless an invalid
              option is encountered, one of the names is not a valid
              shell variable name, or -f is supplied with a name that
              is not a function.

Bob



reply via email to

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