Shell Echo Command – Why /bin/sh -c echo foo Outputs Nothing

argumentscommand lineechoshell

For example, while this works:

$ echo foo
foo

This doesn't:

$ /bin/sh -c echo foo

Whereas this does:

$ /bin/sh -c 'echo foo; echo bar'
foo
bar

Is there an explanation?

Best Answer

From man sh

-c string   If the -c option is present, then commands are read from string. 
            If there are arguments after the string, they are assigned to the
            positional parameters, starting with $0

It means your command should be like this:

 $ sh -c 'echo "$0"' foo 
 foo

Similarly:

$ sh -c 'echo "$0 $1"' foo bar
foo bar

That was the first part to understand; the second case is simple and doesn't need explanation, I guess.