如何find传递给Bash脚本的参数的数量?

如何find传递给Bash脚本的参数的数量?

这是我目前的:

#!/bin/bash i=0 for var in "$@" do i=i+1 done 

还有其他(更好)的方法吗?

参数的数量是$#

在此页上search以了解更多信息: http : //tldp.org/LDP/abs/html/internalvariables.html#ARGLIST

 #!/bin/bash echo "The number of arguments is: $#" a=${@} echo "The total length of all arguments is: ${#a}: " count=0 for var in "$@" do echo "The length of argument '$var' is: ${#var}" (( count++ )) (( accum += ${#var} )) done echo "The counted number of arguments is: $count" echo "The accumulated length of all arguments is: $accum" 

下面是简单的一个 –

猫countvariable.sh

 echo "$@" |awk '{for(i=0;i<=NF;i++); print i-1 }' 

输出:

 #./countvariable.sh 1 2 3 4 5 6 6 #./countvariable.sh 1 2 3 4 5 6 apple orange 8 

该值包含在variables$#