Using Special Parameter Variables
A few special bash shell variables track command line parameters. This section describes what they are and how to use them.
Counting parameters
As you saw in the last section, you should verify command line parameters before using them in your script. For scripts that use multiple command line parameters, this checking can get tedious.
Instead of testing each parameter, you can count how many parameters were entered on the command line. The bash shell provides a special variable for this purpose.
The special $#
variable contains the number of command line parameters included when the script was run. You can use this special variable anywhere in the script, just like a normal variable:
$ cat test8.sh
#!/bin/bash
# getting the number of parameters
#
echo There were $# parameters supplied.
$
$ ./test8.sh
There were 0 parameters supplied.
$
$ ./test8.sh 1 2 3 4 5
There were 5 parameters supplied.
$
$ ./test8.sh 1 2 3 4 5 6 7 8 9 10
There were 10 parameters...