rools
rools

Reputation: 1675

Arguments of sh command ignored when using with ssh

In the following command, the first argument of sh command echo hey is ignored:

$ ssh localhost sh -c 'echo hey; echo ho'

ho

Why?

Upvotes: 3

Views: 412

Answers (1)

jhnc
jhnc

Reputation: 16817

Your commandline is:

ssh localhost sh -c 'echo hey; echo ho'

ssh starts a shell on localhost and passes it the comandline:

sh -c echo hey; echo ho

The shell on localhost sees two commands. Both run fine.

The problem is that the first command is: sh -c echo hey

The option -c tells sh to execute the next argument. The next argument is echo. The extraneous hey argument is ignored.

To fix your problem, either change your quoting or just don't run the redundant shell:

ssh localhost "sh -c 'echo hey; echo ho'"

ssh localhost 'echo hey; echo ho'

The main confusion is probably that ssh concatenates all the non-option arguments it receives into a single string that it passes to the remote shell to execute.

Upvotes: 4

Related Questions