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

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


You need to use single quotes to prevent interpolation happening in your calling shell.

$ bash -c 'echo arg 0: $0, arg 1: $1' arg1 arg2arg 0: arg1, arg 1: arg2

Or escape the variables in your double-quoted string. Which to use might depend on exactly what you want to put in your snippet of code.


Because '$0' and '$1' in your string is replaced with a variable #0 and #1 respectively.

Try :

bash -c "echo arg 0: \$0, arg 1: \$1" arg0 arg1

In this code $ of both are escape so base see it as a string $ and not get replaced.

The result of this command is:

arg 0: arg0, arg 1: arg1

Hope this helps.


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/bashecho 0:$0echo 1:$1echo 2:$2

Then you should call it like this:

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

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

Not like this:

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

Nor like this:

$ bash -c './foo.sh $@' foo "bar baz"0:./foo.sh1:bar2:baz

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