219

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

Are there other (better) ways of doing this?

Zuul
  • 16,217
  • 6
  • 61
  • 88
sabri
  • 2,219
  • 2
  • 14
  • 4

4 Answers4

341

The number of arguments is $#

Search for it on this page to learn more.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • 52
    To clarify: unlike `argc` in C-like languages, `$#` will be `0` if there are no arguments passed to the script, `1` if there is one argument, etc. – Vladimir Panteleev Dec 08 '14 at 11:55
  • 3
    If you `shift` arguments away from its array, `$#` will also be deducted by 1. It will always represent the length of `$@` which is quite handy. – Martin Braun Nov 26 '21 at 22:42
109
#!/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"
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • It helped me too, which I was trying to do is; #!/bin/bash count=0 sum=0 avg=0 for var in "$@" do (( sum += $var )) (( count++ )) done (( avg = sum/$count )) echo "sum of the numbers is: $sum" echo "average of the numbers is: $avg" which helps me to get unkown numbers of args and do some math (you can edit the operand) Thanks for Dennis Williamson, I did it. I'm posting to code because it may be usefull for someone. – Y.Kaan Yılmaz Mar 04 '16 at 11:28
  • 1
    @kaanyılmaz: The only problem with that is that Bash only does integer arithmetic. If you want decimals, you will need to use AWK, `bc`, or something else (ksh93 and zsh can also do decimal math). Your code includes dollar signs for some variables, but not others. You should be consistent about using them or not. Inside `(())` they're not necessary. They are, however, in the `echo` statements. – Dennis Williamson Mar 04 '16 at 17:47
  • I'm not very good with linux, I just modified your code as I needed. It is your code basically – Y.Kaan Yılmaz Mar 04 '16 at 19:19
24

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."

Michael Brux
  • 4,256
  • 1
  • 20
  • 21
-3

Below is the easy one -

cat countvariable.sh

echo "$@" | awk '{print NF}'

Output :

#./countvariable.sh 1 2 3 4 5 6
6
#./countvariable.sh 1 2 3 4 5 6 apple orange
8
VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34