在脚本中通过SSH使用反引号(`) [英] Using backticks (`) over ssh in a script
问题描述
我有一个命令,当我在本地运行时,我使用反引号获取其中包含的命令的输出,我想通过ssh发送此命令(这不是实际命令,而是一个简化的示例)
I have a command that, when I run locally, I use backticks in to get that output of the commands contained within them and I want to send this over ssh (this isn't the actual command but a shortened example)
cat `ls -alr | grep 'someregex'`
我尝试使用
ssh -f hostname cat `ls | grep 'someregex'`
但是这不符合我的预期,而是在本地执行反引号,有人知道如何解决吗?
however this doesn't act as I was expecting and instead executes the backticks locally, does anyone know a way around this?
推荐答案
尝试将命令用引号引起来:
Try enclosing the command in quotes:
ssh -f hostname 'cat `ls | grep "someregex"`'
请注意,内部引号必须替换为双引号.
Note that the inner quotes have to be replaced with double quotes.
此外,请注意,您不能将整个命令用双引号引起来,因为bash会在调用ssh
之前在本地扩展子shell.例如,比较以下命令:
Also, note that you can't enclose the whole command in double quotes, because bash will expand the subshell locally before it invokes ssh
. For example, compare the following commands:
$ echo "`echo foo`"
foo
$ echo '`echo foo`'
`echo foo`
这篇关于在脚本中通过SSH使用反引号(`)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!