[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: |
Wed, 26 Dec 2018 12:10:24 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
On 12/23/18 12:01 PM, Bize Ma wrote:
> Chet Ramey (<chet.ramey@case.edu <mailto:chet.ramey@case.edu>>) wrote:
>
> >
> > While this works:
> >
> > var=(hello); echo "${var[ ~0]}"
> > hello
>
> Because negative array subscripts count backwards from the end of the
> array.
>
>
> Doh!, yes. And, because of that: "${var[-1]}"
> should give the *last* element of array "var", shouldn't it?
Yes, if it's an array variable.
> Consequently, this happens:
>
> $ unset var; var[0]=77; echo "${var[0]}"; echo "${var[-1]}"
> 77
> 77
>
> The only value in var is at index 0, which means it is also the *last* value.
Correct.
>
> The point being that a variable which has an scalar value "var=hello"
> should act (for most practical cases) as an array for which only the
> value at address 0 has been defined.
>
> Both command line above should have printed "hello".
No. 0 is the only valid subscript for a non-array variable. The difference
between bash and other shells that implement this feature is that bash
warns about negative subscripts.
--
``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/