Re: V+=1 doesn't work if V is a reference to an integer array element
From:
Chet Ramey
Subject:
Re: V+=1 doesn't work if V is a reference to an integer array element
Date:
Wed, 13 Jan 2021 16:13:55 -0500
User-agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.6.0
On 1/13/21 1:11 PM, Léa Gris wrote:
echo $BASH_VERSION
5.0.17(1)-release
unset a b
declare -ai a=(1)
declare -in b="a[0]"
The `-i' forces arithmetic evaluation, which makes this expand to the
equivalent of `declare -n b=1'. That's an invalid name for a nameref,
which you'd see if you used `declare -n b=1' instead. The assignment
error causes `declare' to return an non-zero status, but the error
message checks the original value, not the result of arithmetic
evaluation, and the `a[0]' is valid.
I guess I never thought people would try to use integer variables as
namerefs, since nameref values can't be digits.
--
``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/