Length of an Array

To get length of an array in Bash, use the following syntax.

${#arrayname[@]}

The above expression returns the number of elements in the array.

Example

Length of an Array

In the following script, we initialise an array arr with three string values and then get the length of the array programmatically.

Example.sh

arr=("apple" "banana" "cherry")
len=${#arr[@]}
echo "Length of Array : $len"

Output

Length of Array : 3
ADVERTISEMENT

Conclusion

In this Bash Tutorial, we have learnt how to get length of an array in Bash shell.