0

How can I inject multiple arguments into a Bash shell script? For example, I have the following code.

script.sh
#!/bin/bash
dir1="foo"
dir2="bar"
echo "Comparing $dir1 to $dir2..."

Now, I want to run this same script again. Only this time, replacing "foo" and "bar" with "baz" and "bat". Like so.

script.sh
#!/bin/bash
dir1="baz"
dir2="bat"
echo "Comparing $dir1 to $dir2..."

How can I accomplish all this with a single while loop?

Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
  • how are you providing values to your script? do you have multiple variables in your script and you want to go over them with the while loop? you can just use echo "Comparing $1 to $2..." and call script with script.sh foo bar, and then script.sh baz bat, if you want script to accept your inputs, and not some predefined list. – ralz Dec 11 '18 at 12:10
  • If this is bash (as tagged), the a simple solution is a pair of arrays, e.g. `src=(foo baz); dest=(bar bat);` and then simply use a C-style `for` loop to loop over the indexes `echo "Comparing ${src[i]} ${dest[i]}"`. – David C. Rankin Dec 11 '18 at 12:15

0 Answers0