0

Given ./mysh0:

#!/bin/bash

exec ./mysh1 $*

And ./mysh1:

#!/bin/bash

echo $1
echo $2
echo $3

How do I call mysh0 such that the arguments to mysh1 and what's eventually printed are "A", "B 2" and "C"?

Calling this as ./mysh0 A "B 2" C does not work.

4
  • 2
    Using "$@" instead of $*, note the quotes. See also: unix.stackexchange.com/q/41571/38906
    – cuonglm
    Commented Dec 10, 2015 at 3:15
  • Hmm. Anyway working around it? I'm afraid ./mysh0 is an intermediate script that I do not own.
    – jvliwanag
    Commented Dec 10, 2015 at 3:22
  • 2
    No, you can not. You must control the mysh0, because it decided how to pass argument to mysh1.
    – cuonglm
    Commented Dec 10, 2015 at 3:27
  • Ok got it! Mind posting this as an answer so I can mark this as answered?
    – jvliwanag
    Commented Dec 10, 2015 at 4:01

1 Answer 1

3

You must use "$@" instead of $*:

exec ./mysh1 "$@"

That's the right way to expand all positional arguments as separated words.

When you use $*, all positional arguments was concatenated in to one long string, with the first value of IFS as separator, which default to a whitespace, you got A B 2 C.

Now, because you use $* without double quote (which can lead to security implications and make your script choked), the shell perform split+glob on it. The long string you got above was split into four words, A, B, 2 and C.

Therefore, you actually passed four arguments to mysh1 instead of three.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .