Array Length
In this page:
Array Length
${#array[@]} gives the number of elements currently in the array. This works for both indexed and associative arrays. It's commonly used as the loop bound when iterating by numeric index instead of with foreach-style iteration.
Note: ${#array[@]} counts elements; ${#array[0]} (no @) instead gives the length of just the first element's string.
Example: Array Length
#!/bin/bash
items=("pen" "notebook" "eraser" "ruler")
echo "Number of items: ${#items[@]}"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: