bash 如何在shell中找到数组的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1886374/
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-09 18:46:55  来源:igfitidea点击:

How to find the length of an array in shell?

arraysbashshell

提问by Arunachalam

How do I find the length of an array in shell?

如何在shell中找到数组的长度?

For example:

例如:

arr=(1 2 3 4 5)

And I want to get its length, which is 5 in this case.

我想得到它的长度,在这种情况下是 5。

回答by ghostdog74

$ a=(1 2 3 4)
$ echo ${#a[@]}
4

回答by codeforester

From Bash manual:

从 Bash手册

${#parameter}

The length in characters of the expanded value of parameter is substituted. If parameter is ‘' or ‘@', the value substituted is the number of positional parameters. If parameter is an array name subscripted by ‘' or ‘@', the value substituted is the number of elements in the array. If parameter is an indexed array name subscripted by a negative number, that number is interpreted as relative to one greater than the maximum index of parameter, so negative indices count back from the end of the array, and an index of -1 references the last element.

${#parameter}

参数的扩展值的字符长度被替换。如果参数是“ ”或“@”,则替换的值是位置参数的数量。如果参数是一个以“”或“@”下标的数组名称,则替换的值是数组中的元素数。如果参数是一个下标为负数的索引数组名称,则该数字被解释为相对于大于参数的最大索引的一个,因此负索引从数组的末尾开始计数,并且索引 -1 引用最后一个元素。

Length of strings, arrays, and associative arrays

字符串、数组和关联数组的长度

string="0123456789"                   # create a string of 10 characters
array=(0 1 2 3 4 5 6 7 8 9)           # create an indexed array of 10 elements
declare -A hash
hash=([one]=1 [two]=2 [three]=3)      # create an associative array of 3 elements
echo "string length is: ${#string}"   # length of string
echo "array length is: ${#array[@]}"  # length of array using @ as the index
echo "array length is: ${#array[*]}"  # length of array using * as the index
echo "hash length is: ${#hash[@]}"    # length of array using @ as the index
echo "hash length is: ${#hash[*]}"    # length of array using * as the index

output:

输出:

string length is: 10
array length is: 10
array length is: 10
hash length is: 3
hash length is: 3

Dealing with $@, the argument array:

处理$@,参数数组:

set arg1 arg2 "arg 3"
args_copy=("$@")
echo "number of args is: $#"
echo "number of args is: ${#@}"
echo "args_copy length is: ${#args_copy[@]}"

output:

输出:

number of args is: 3
number of args is: 3
args_copy length is: 3

回答by unwind

Assuming bash:

假设 bash:

~> declare -a foo
~> foo[0]="foo"
~> foo[1]="bar"
~> foo[2]="baz"
~> echo ${#foo[*]}
3

So, ${#ARRAY[*]}expands to the length of the array ARRAY.

因此,${#ARRAY[*]}扩展到数组的长度ARRAY

回答by Rahul Mahajan

in tcsh or csh:

在 tcsh 或 csh 中:

~> set a = ( 1 2 3 4 5 )
~> echo $#a
5

回答by harm

In the Fish Shellthe length of an array can be found with:

Fish Shell 中,可以通过以下方式找到数组的长度:

$ set a 1 2 3 4
$ count $a
4

回答by Mansur Ali

this works well for me

这对我很有效

    arglen=$#
    argparam=$*
    if [ $arglen -eq '3' ];
    then
            echo Valid Number of arguments
            echo "Arguments are $*"
    else
            echo only four arguments are allowed
    fi

回答by user3771114

For those who still searching a way to put the length of an array into a variable:

对于那些仍在寻找将数组长度放入变量的方法的人:

foo=$(echo ${'ARRAY[*]}