From the bash man-page:
-c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.
So I created a "script" file named foo.sh, containing the single line
echo par 0 is $0, par 1 is $1
, set it to executable, and invoked it as
bash -c ./foo.sh x y
I expected to see par 0 is x, par 1 is y, but it was printed par 0 is ./foo.sh, par 1 is.
In what respect did I misunderstand the man-page?
UPDATE Perhaps to clarify it (since my question seems to have given rise to some confusion): My goal is to execute foo.sh, but making it believe that its real name is not foo.sh, but x.
The inline script (the ./foo.sh argument just after the -c option) is not passing any argument to ./foo.sh:
bash -c ./foo.sh x y
This is the inline script:
./foo.sh
This first level script indeed receives x as argument $0 and y as argument $1.
But it does not pass any argument when invoking foo.sh.
It explains why foo.sh only see itself as the $0 argument, and nothing as argument $1.
Now try with this
bash -c './foo.sh "$@"' x y
And its output is:
par 0 is ./foo.sh, par 1 is y
This is because "$@" does not expand argument $0 which is the script itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With