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


Reply via email to