How to pass parameters to a Bash script?

I have a bash script as below, and I want it to read two dates as parameters, for example: myshell date1 date2. How do I assign parameters to variables date1 and date2?

sed "s/$date1/$date2/g" wlacd_stat.xml >tmp.xml
mv tmp.xml wlacd_stat.xml

Solution 1:

You use $1, $2 in your script. E.g:

date1="$1"
date2="$2"
sed "s/$date1/$date2/g" wlacd_stat.xml >temp.xml
mv temp.xml wlacd_stat.xml

Solution 2:

To iterate over the parameters, you can use this shorthand:

#!/bin/bash
for a
do
    echo $a
done

This form is the same as for a in "$@".

Solution 3:

Bash arguments are named after their position.

Moreover, if you need to handle one argument after the other, you can shift them and always use $1:

while [ $# -gt 0 ]
do
    echo $1
    shift
done