Array length

Arrays and collections / Arrays

numbers=(1 2 3)
length=${#numbers[@]}
#length is 3

#without [@] it is the length of the first element as a string
words=("hello" "hi")
firstLength=${#words}
#firstLength is 5

#the length counts the elements, not the last index plus one
sparse=([0]=1 [10]=2)
sparseLength=${#sparse[@]}
#sparseLength is 2

echo "length is $length"
echo "firstLength is $firstLength"
echo "sparseLength is $sparseLength"