如何找到传递给Bash脚本的参数数?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (16)

如何找到传递给Bash脚本的参数数?

这就是我目前的情况:

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

还有其他(更好的)方法来做这件事吗?

提问于
用户回答回答于
#!/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"
用户回答回答于

扫码关注云+社区