ssh "bash -c" bug

Peter Stuge peter at stuge.se
Thu Feb 21 03:00:11 EST 2013


Олохтонов Владимир wrote:
> The arguments of the first command given to "bash -c" over ssh is
> being ignored.
>
> xxx at yyy:~$ ssh xxx at localhost bash -c 'echo foo; echo bar'
>
> bar

You should study shell quoting and the SSH protocol.

The ssh command is being run in an interactive shell. Your quotes
tell this shell that it should pass "echo foo; echo bar" excluding
the quotes as a single parameter to the ssh command.

As you can see in the SSH protocol, the command to be executed by the
server is sent as a single string. In your example that string is
"bash -c echo foo; echo bar" excluding the quotes.

Try what happens when you run that command directly:

$ bash -c echo foo; echo bar

bar
$ 

..and notice that the result is the same. Try to understand why.

Now think about what you must do to get your desired result.


Understanding quoting is fundamental for actually using the shell, as
opposed to copypaste commands from blogs or entering random
characters and hoping for the best. There is plenty of good material
on the subject, you can start with the bash man page. man bash then
search for QUOTING in caps. Shell quoting is also covered in
countless books, and probably also in some video tutorials on youtube
if that is what you prefer. And of course you must always be aware of
which shell is being used where, since quoting works differently in
different shells.


//Peter


More information about the openssh-unix-dev mailing list