[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Error on arithmetic evaluation of `~0`.
From: |
Chet Ramey |
Subject: |
Re: Error on arithmetic evaluation of `~0`. |
Date: |
Sat, 22 Dec 2018 12:00:18 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
On 12/19/18 10:31 PM, Bize Ma wrote:
> This is the third time I am reporting this issue.
Not really, but let's go on.
>
> This fails:
>
> var=(hello); echo "${var[~0]}"
> syntax error: operand expected ...
Yes. The comment in the code says:
"Right now, the code
suppresses tilde expansion when expanding in a pure arithmetic
context, but allows it when expanding an array subscript. This is
for backwards compatibility, but I figure nobody's relying on it"
I suppose backwards compatibility here is less important at this point.
>
> While this works:
>
> var=(hello); echo "${var[ ~0]}"
> hello
Because negative array subscripts count backwards from the end of the
array.
> It is also interesting that this fails:
>
> var=hello; echo "${var[ ~0]}"
> bash: var: bad array subscript
>
> Isn't `var[0]` valid and equivalent to `var` ?
Yes, but ~0 (-1) is not the same as 0.
> This was "supposed" to be resolved in a dev version,
> but is still present on bash 5.
The other arithmetic contexts you reported (the "pure" arithmetic contexts
the comment above references) were changed; this was left for backwards
compatibility. Like I said above, it looks like it's time to deemphasize
that.
Chet
--
``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/