Reputation: 847
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
Upvotes: 56
Views: 95764
Reputation: 4733
$0
$1
$2
And so on will contain the script name, then the first and the second line argument.
Upvotes: 6
Reputation: 342363
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
Upvotes: 79
Reputation: 360075
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 "$@"
.
Upvotes: 9
Reputation: 67829
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
Upvotes: 8