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

后端 未结 5 1833
暖寄归人
暖寄归人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 08:50

    Below is the easy one -

    cat countvariable.sh

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

    Output :

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

提交回复
热议问题