gho*_*g74 76
$ a=(1 2 3 4)
$ echo ${#a[@]}
4
Run Code Online (Sandbox Code Playgroud)
unw*_*ind 18
假设bash:
~> declare -a foo
~> foo[0]="foo"
~> foo[1]="bar"
~> foo[2]="baz"
~> echo ${#foo[*]}
3
Run Code Online (Sandbox Code Playgroud)
因此,${#ARRAY[*]}扩展到数组的长度ARRAY.
cod*_*ter 17
从Bash 手册:
$ {#参数}
参数展开值的字符长度被替换.如果参数是' '或'@',则替换的值是位置参数的数量.如果parameter是由' '或'@' 下标的数组名,则替换的值是数组中元素的数量.如果参数是由负数下标的索引数组名称,则该数字被解释为相对于大于参数的最大索引的数字,因此负数索引从数组的末尾开始计数,索引-1引用最后一个元件.
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
Run Code Online (Sandbox Code Playgroud)
输出:
string length is: 10
array length is: 10
array length is: 10
hash length is: 3
hash length is: 3
Run Code Online (Sandbox Code Playgroud)
$@参数数组:set arg1 arg2 "arg 3"
args_copy=("$@")
echo "number of args is: $#"
echo "number of args is: ${#@}"
echo "args_copy length is: ${#args_copy[@]}"
Run Code Online (Sandbox Code Playgroud)
输出:
number of args is: 3
number of args is: 3
args_copy length is: 3
Run Code Online (Sandbox Code Playgroud)
在鱼壳中,可以通过以下方式找到数组的长度:
$ set a 1 2 3 4
$ count $a
4
Run Code Online (Sandbox Code Playgroud)
这对我来说效果很好:
arglen=$#
argparam=$*
if [ $arglen -eq '3' ];
then
echo Valid Number of arguments
echo "Arguments are $*"
else
echo only four arguments are allowed
fi
Run Code Online (Sandbox Code Playgroud)