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

后端 未结 5 1825
暖寄归人
暖寄归人 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 08:56

    to add the original reference:

    You can get the number of arguments from the special parameter $#. Value of 0 means "no arguments". $# is read-only.

    When used in conjunction with shift for argument processing, the special parameter $# is decremented each time Bash Builtin shift is executed.

    see Bash Reference Manual in section 3.4.2 Special Parameters:

    • "The shell treats several parameters specially. These parameters may only be referenced"

    • and in this section for keyword $# "Expands to the number of positional parameters in decimal."

提交回复
热议问题