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


Groups > gnu.bash.bug > #14959 > unrolled thread

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

Started byChet Ramey <chet.ramey@case.edu>
First post2018-12-26 12:10 -0500
Last post2018-12-26 12:10 -0500
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#14959 — Re: Error on arithmetic evaluation of `~0`.

FromChet Ramey <chet.ramey@case.edu>
Date2018-12-26 12:10 -0500
SubjectRe: Error on arithmetic evaluation of `~0`.
Message-ID<mailman.6319.1545844236.1284.bug-bash@gnu.org>
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/

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web