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

sabri picture sabri · Dec 12, 2010 · Viewed 180.6k times · Source

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?

Answer

zsalzbank picture zsalzbank · Dec 12, 2010

The number of arguments is $#

Search for it on this page to learn more: http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST