I have a big script (call it test
) that, after stripping out the unrelated parts, comes down to just this using which I can explain my question:
#!/bin/bash
bash -c "$@"
This doesn't work as expected. E.g. ./test echo hi
executes the only the echo
and the argument disappears!
Testing with various inputs I can see only $1
is passed to bash -c ...
and rest are discarded.
But if I use a variable like:
#!/bin/bash
cmd="$@"
bash -c "$cmd"
it works as expected for all inputs.
Questions:
1) I would like to understand why the double quotes don't "pass" the entire command line arguments to bash -c ...
. What am I missing here (that it works perfectly fine when using an intermediate variable)?
2) Why does bash discard the rest of the arguments (except $1
) without any error messages?
For example:
bash -c "ls" -l -a hi hello blah
simply runs echo
and hi hello blah
doesn't result in any errors at all?
(If possible, please refer to the bash grammar where this behaviour is documented).