Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments of sh command ignored when using with ssh

Tags:

sh

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?

like image 250
rools Avatar asked Dec 01 '25 07:12

rools


1 Answers

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.

like image 55
jhnc Avatar answered Dec 02 '25 22:12

jhnc