Bash 关联数组大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18185920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:12:48  来源:igfitidea点击:

Bash associative array size

linuxbashassociative-array

提问by wick

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

有没有办法在 bash 中获取关联数组的大小:

declare -A array

...without iterating through the elements?

...不遍历元素?

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

感兴趣的大小是:只是元素数量和它消耗的内存量?

回答by devnull

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

${#array[@]}将返回数组的大小。

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

回答by paul

You can use ${#array[@]}to get the number of elements.

您可以使用${#array[@]}来获取元素的数量。

I don't think it is possible to get the amount of memory it consumes however.

但是,我认为不可能获得它消耗的内存量。