I can't seem to use the Bash “-c” option with arguments after the “-c” option string

前端 未结 4 1810
感情败类
感情败类 2020-12-02 15:31

The man page for Bash says, regarding the -c option:

-c string If the -c option is present, then comma

4条回答
  •  甜味超标
    2020-12-02 16:28

    martin is right about the interpolation: you need to use single quotes. But note that if you're trying to pass arguments to a command that is being executed within the string, you need to forward them on explicitly. For example, if you have a script foo.sh like:

    #!/bin/bash
    echo 0:$0
    echo 1:$1
    echo 2:$2
    

    Then you should call it like this:

    $ bash -c './foo.sh ${1+"$@"}' foo "bar baz"
    0:./foo.sh
    1:bar baz
    2:
    

    Or more generally bash -c '${0} ${1+"$@"}' [argument]...

    Not like this:

    $ bash -c ./foo.sh foo "bar baz"
    0:./foo.sh
    1:
    2:
    

    Nor like this:

    $ bash -c './foo.sh $@' foo "bar baz"
    0:./foo.sh
    1:bar
    2:baz
    

    This means you can pass in arguments to sub-processes without embedding them in the command string, and without worrying about escaping them.

提交回复
热议问题