Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14959

Re: Error on arithmetic evaluation of `~0`.

From Chet Ramey <chet.ramey@case.edu>
Newsgroups gnu.bash.bug
Subject Re: Error on arithmetic evaluation of `~0`.
Date 2018-12-26 12:10 -0500
Organization ITS, Case Western Reserve University
Message-ID <mailman.6319.1545844236.1284.bug-bash@gnu.org> (permalink)
References <CAFra36gW8Csu-84oRLV7QsR7PbiMYq6g7WnMktjfnrSdTys38g@mail.gmail.com> <25e8adf0-96d5-1e38-faea-e7620ba3f846@case.edu> <CAFra36gzzbJLjqt9OYtJUuJara3KR2+ew-Gj3y2mA2L4X8CRyw@mail.gmail.com>

Show all headers | View raw


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/

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Error on arithmetic evaluation of `~0`. Chet Ramey <chet.ramey@case.edu> - 2018-12-26 12:10 -0500

csiph-web