Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
myshell date1 date2
date1
date2
sed "s/$date1/$date2/g" wlacd_stat.xml >tmp.xml mv tmp.xml wlacd_stat.xml
You use $1, $2 in your script. E.g:
$1
$2
date1="$1" date2="$2" sed "s/$date1/$date2/g" wlacd_stat.xml >temp.xml mv temp.xml wlacd_stat.xml
Add a comment
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 "$@".
for a in "$@"
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
$0 $1 $2
And so on will contain the script name, then the first and the second line argument.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.