How do I find the number of arguments passed to a Bash script?

后端 未结 5 1830
暖寄归人
暖寄归人 2020-12-04 08:31

How do I find the number of arguments passed to a Bash script?

This is what I have currently:

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


        
5条回答
  •  借酒劲吻你
    2020-12-04 09:06

    #!/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"
    

提交回复
热议问题