0

I am new to programming, so plz bear with the way I try to explain my problem (also any help regarding how to elegantly phrase the tile is welcome).

I have a bash script (say for example script1.sh ) that takes in arguments a, b and c(another script). Essentially, argument c for script1.sh is the name of another script (let's say script2.sh). However, script2.sh takes in arguments d,e and f. So my question is, how do I pass arguments to script1.sh ?? (example, ./script1.sh -a 1 -b 2 -c script2.sh -d 3 -e 4 -f 5)

Sorry in advance if the above does not make sense, not sure how else to phrase it...

Mc Missile
  • 715
  • 11
  • 25
  • Check if this helps: https://stackoverflow.com/questions/16988427/calling-one-bash-script-from-another-script-passing-it-arguments-with-quotes-and – samthegolden Jan 28 '20 at 09:29

1 Answers1

1

You should use "" for that

./script1.sh -a 1 -b 2 -c "script2.sh -d 3 -e 4 -f 5"

Try script1.sh with this code

#!/bin/bash

for arg in "$@"; { # loop through all arguments passed to the script
    echo $arg
}

The output will be

$ ./script1.sh -a 1 -b 2 -c "script2.sh -d 3 -e 4 -f 5"
-a
1
-b
2
-c
script2.sh -d 3 -e 4 -f 5

But if you run this

#!/bin/bash

for arg in $@; { # no double quotes around $@
    echo $arg
}

The output will be

$ ./script1.sh -a 1 -b 2 -c "script2.sh -d 3 -e 4 -f 5"
-a
1
-b
2
-c
script2.sh
-d
3

4
-f
5

But there is no -e why? Coz echo supports argument -e and use it.

Ivan
  • 6,188
  • 1
  • 16
  • 23