ssh 命令和引号 [英] ssh command and quotes

查看:88
本文介绍了ssh 命令和引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行ssh命令后跟带引号的远程命令和不带引号的远程命令有什么区别?

What is the difference between execution ssh command followed by remote command with and quotes and the remote command without quotes?

例如有什么区别:

ssh remotehost tar xf archivename.tar

ssh remotehost "tar xf archivename.tar"

或者有什么区别:

ssh remotehost <path to script>/myscript.sh param

ssh remotehost "<path to script>/myscript.sh param"

推荐答案

尾随参数组合成一个字符串,作为参数传递给登录 shell 的 -c 选项远程机器.在您的第一个示例中,几乎没有区别.但是,请考虑以下事项:

The trailing arguments are combined into a single string which is passed to as an argument to the -c option of your login shell on the remote machine. In your first example, there is little difference. However, consider the following:

$ ssh remotehost echo *

这里,*在本地展开,结果传给远程机器.在我的机器上,我的工作目录由一个包含撇号(单引号)的条目组成,因此远程主机尝试运行像

Here, * is expanded locally, and the result is passed to the remote machine. On my machine, my working directory consisted of a single entry containing an apostrophe (single quote), so the remote host tried to run a command like

bash -c "echo chepner's folder"

这当然导致了错误.

$ ssh remotehost "echo *"

现在远程主机得到一个文字 *,但在传递给 bash -c 之前它会受到 shell 扩展的影响.结果,您输出了远程主机上主目录中的所有文件.

Now the remote host gets a literal *, but it is subject to shell expansion before being passed to bash -c. As a result, you output all the files in your home directory on the remote host.

$ ssh remotehost "echo \*"

最后,远程 shell 在它的输入中得到一个 引用 星号,所以你得到一个文本 * 作为输出.

Finally, the remote shell gets a quoted asterisk in its input, so you get a single literal * as output.

这篇关于ssh 命令和引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆