Happy new year! 2021年1月2日(土) 20:44 felix <fe...@f-hauri.ch>: > ( Under 4.2.37(1)-release, I get: ``-bash: a[$i]: bad array subscript'' ) > > But with ``my'' 5.0.3(1)-release, as with new 5.1.4(1)-release, no > error...
Negative array subscripts are indeed a new feature of Bash 4.3. From the changelog (http://tiswww.case.edu/php/chet/bash/CHANGES): > This document details the changes between this version, > bash-4.3-alpha, and the previous version, bash-4.2-release. > > > [...] > > 3. New Features in Bash > > [...] > > x. The shell now allows assigning, referencing, and unsetting > elements of indexed arrays using negative subscripts (a[-1]=2, > echo ${a[-1]}) which count back from the last element of the > array. ---- > Where 5th value is 45 instead of -12 and 6th is 36 instead of -21 > 45 + 12 = 57, 36 + 21 = 57 ... (highest submited value was 56!)?? The index -1 corresponds to the highest index, so a negative index "-O" corresponds to the index "H-O+1" where H is the highest index, so the behavior that -12 and -21 correspond to 56-12+1=45 and 56-21+1=36, respectively, is the correct one. Or in another way of thinking, when the highest index is 56, the number of elements in a hypothetical dense array a[0]..a[56] is N=57, so -12 and -21 correspond to N-12=45 and N-21=36. -- Koichi