bug-bash
[Top][All Lists]
Advanced

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

Re: Empty array referenced by indirection reports unbound variable


From: Chet Ramey
Subject: Re: Empty array referenced by indirection reports unbound variable
Date: Wed, 7 Apr 2021 14:44:31 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.0

On 4/7/21 10:39 AM, konsolebox wrote:
On Wed, Apr 7, 2021 at 9:25 PM Chet Ramey <chet.ramey@case.edu> wrote:

On 4/5/21 4:45 PM, konsolebox wrote:
set -u
array=()
__ref=array[@]
: "${array[@]}" # Reports nothing

This is a special case, mirroring the special case that POSIX carved out
for $@ and $* in 2009.

: "${!__ref}" # Unbound variable

But this is not. Bash looks for __ref, finds it with a value of array[@],
and attempts to expand that. It expands to null (internally), since there
are no array elements, which becomes the value of the parameter expansion.

Since the expansion (${!__ref}) resulted in the null string, `set -u' kicks
in and bash reports a fatal error.

But that doesn't seem consistent since `set -- "${!__ref}"; echo "$#"`
shows 0, meaning it didn't expand to a null string but practically
nothing.

The null string *is* nothing. Literally. 0x0. It's an ex-parrot.

Does that mean internally ${!__ref} expands to null string
but checks for a flag if it's an array expansion before the actual
resulting expansion?

No. It just expands to a null string.

If that's the case shouldn't it be the same when
reporting unbound variable?

Indirection does not check whether or not the variable it's indirecting
is $@/$* or ${array[@/*]}. It simply goes by the return value.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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