56

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
0

4 Answers 4

80

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
Sign up to request clarification or add additional context in comments.

Comments

9

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 "$@".

Comments

8

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

Comments

6
$0
$1
$2

And so on will contain the script name, then the first and the second line argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.