Bash associative array size

wick picture wick · Aug 12, 2013 · Viewed 14.4k times · Source

Is there a way to get size of associative array in bash:

declare -A array

...without iterating through the elements?

The size of interest is both: just number of elements, and memory amount it consumes?

Answer

devnull picture devnull · Aug 12, 2013

${#array[@]} would return you the size of the array.

$ declare -A array
$ array[foo]='something'
$ array[bar]='blah'
$ array[42]='nothing'
$ echo ${#array[@]}
3