Meanwhile you may use functions to setup your variables something along those lines.
PW$ function first-index { echo $1; } PW$ function last-index { shift $(($#-1)) ; echo $1; } PW$ declare -a array=([5]="hello" [11]="world" [42]="here") PW$ declare -i first_index=$(first-index ${!array[@]}) PW$ declare -i last_index=$(last-index ${!array[@]}) PW$ echo $first_index $last_index 5 42 On Thu, May 6, 2021 at 1:24 PM Léa Gris <lea.g...@noiraude.net> wrote: > Currently, to know the index of an array's indexes (example: first or > last index), it needs an intermediary index array: > > > #!/usr/bin/env bash > > > > declare -a array=([5]=hello [11]=world [42]=here) > > declare -ai indexes=("${!array[@]}") > > declare -i first_index=${indexes[*]:0:1} > > declare -i last_index=${indexes[*]: -1:1} > > declare -p array indexes first_index last_index > > Which prints: > > declare -a array=([5]="hello" [11]="world" [42]="here") > > declare -ai indexes=([0]="5" [1]="11" [2]="42") > > declare -i first_index="5" > > declare -i last_index="42" > > It would be convenient to be able to index directly with this syntax: > > declare -i first_index=${!array[@]:0:1} > declare -i last_index=${!array{@}: -1:1} > > > -- > Léa Gris > > >