In bash, is it possible to get the index of the last element of an array (that might be sparse) without looping through the entire array like so:
a=( e0 e1 ... )
i=0
while [ "$i" -lt $(( ${#a[@]} - 1 )) ]
do
let 'i=i+1'
done
echo "$i"
Since at least bash v 4.2, I can get the value of the last element in an array using
e="${array[-1]}"
but that will not get me the positive index since other elements may have the same value.
i=$((${#a[@]}-1))
.$(( ${#a[@]} - 1 ))
will give the number of elements in the array. That may or may not be the same as the index of the last element.