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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
jvliwanag
  • 103
  • 3

1 Answers1

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.

cuonglm
  • 150,973
  • 38
  • 327
  • 406