[Top][All Lists]

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

Re: declare [-+]n behavior on existing (chained) namerefs

From: Chet Ramey
Subject: Re: declare [-+]n behavior on existing (chained) namerefs
Date: Thu, 5 May 2016 15:43:40 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Thunderbird/45.0

On 4/28/16 9:49 PM, Grisha Levit wrote:

> In a slightly different version, with `declare -n r; r=a', the function
> exits with code 1 after the `r=a' statement:
> $ declare -nt r=a; f() { declare a; declare -n r; r=a; declare -p a r; };
> f; echo $?
> 1

In this case, you create a self-referencing local nameref, which ends up
resolving to nothing, which causes an assignment error, which results in
function execution being aborted.  You create the self-referencing local
nameref because bash follows the nameref chain for `r', and resolves it
to `a'.  It's as Piotr surmised: as if you ran `typeset -n a; a=a'.

When asked to create a local variable that shadows a nameref from a
different context, declare needs to throw away the results of that nameref
chain search and just create a local variable.

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

reply via email to

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