简体   繁体   English

与 ssh 一起使用时忽略 sh 命令的 Arguments

[英]Arguments of sh command ignored when using with ssh

In the following command, the first argument of sh command echo hey is ignored:在以下命令中,忽略sh命令echo hey的第一个参数:

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

ho

Why?为什么?

Your commandline is:你的命令行是:

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

ssh starts a shell on localhost and passes it the comandline: ssh在 localhost 上启动 shell 并将其传递给命令行:

sh -c echo hey; echo ho

The shell on localhost sees two commands. localhost 上的 shell 看到两个命令。 Both run fine.两者都运行良好。

The problem is that the first command is: sh -c echo hey问题是第一个命令是: sh -c echo hey

The option -c tells sh to execute the next argument.选项-c告诉sh执行下一个参数。 The next argument is echo .下一个参数是echo The extraneous hey argument is ignored.无关的hey参数被忽略。

To fix your problem, either change your quoting or just don't run the redundant shell:要解决您的问题,请更改您的报价或不运行多余的 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.主要的混淆可能是ssh将它接收到的所有非选项 arguments 连接成一个字符串,然后传递给远程 shell 执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM