On 12/23/18 12:01 PM, Bize Ma wrote: > Chet Ramey (<chet.ra...@case.edu <mailto:chet.ra...@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 c...@case.edu http://tiswww.cwru.edu/~chet/