bashpositional-parameter

Confusion about -c option in bash


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.


Solution

  • You would see the behaviour you wanted with this :

    bash -c "$(cat foo.sh)" x y