Arguments to a shell script are called positional parameters.
They are accessed positionally and expanded (to get the value) with a $.
$1 is the value of first argument, $2 is the second argument, $3 is the third argument, $4 is the fourth argument, ...
$0 is the command that has been run.
$# means the number of arguments ($0 is not counted).
$* expands all the arguments.
Do you need to use quotes around $1? Why or why not? If so, which type of quotes?
Whitespace
Note that in general, whitespace is tricky and this holds for command-line arguments too.
For instance:
arg=1
echo "There are $# arguments"
for f in $*
do
echo "arg is: $f"
((arg++)) # ooo - math!
done
What happens when you run the script with these arguments? a b c cat
How about these? "This is a test" cat
"${@}" has special meaning to the shell. It means "all of the arguments", where each one
is in quotes.
Note: the curly-braces are optional in this example. They indicate the start/end of the evaluation performed by the $. This is required in certain cases: $val1 versus ${val}1
So a better way to write the for is: for f in "${@}"
Misc Tips for Argument Processing
The shift command will shift each argument up one position; $2 becomes $1, $3 becomes $2, ... $1 is lost.
The getopts command will allow you to write scripts that fully implement the standard
Linux syntax for command-line arguments and flags.