bug-bash
[Top][All Lists]
Advanced

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

Re: V+=1 doesn't work if V is a reference to an integer array element


From: Léa Gris
Subject: Re: V+=1 doesn't work if V is a reference to an integer array element
Date: Thu, 14 Jan 2021 16:31:26 +0100
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Le 14/01/2021 à 16:15, Chet Ramey écrivait :
On 1/13/21 4:40 PM, Léa Gris wrote:
Le 13/01/2021 à 22:13, Chet Ramey écrivait :
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.


Sorry Chet, this does not make sense to me. There is no error message.

OK, let's be clear that this sequence of commands from your message is what
we're talking about:

unset a b
declare -ai a=(1)
declare -in b="a[0]"
declare -p a b

I get this result:

declare -ai a=([0]="1")
x4: line 4: declare: b: not found

and I explained why there is no variable `b'.


Not the same issue and not exactly same sequence of commands:

unset a; unset -n b
declare -ai a=(1)
declare -n b="a[0]" # No i flag at that point for b
typeset -ni b # Now b also has a int flag alongside the nameref
b+=2 # This is only allowed if b has the int flag
declare -p a b

As Oğuz wrote:
Seems to be another bug. `bind_variable_internal' calls both
`assign_array_element' and `make_variable_value' with ASS_APPEND in flags (
https://git.savannah.gnu.org/cgit/bash.git/tree/variables.c#n3140 ), the
latter returns 6 (4 + 2), the former adds it to a[0]'s value.


--
Léa Gris




reply via email to

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