嵌套引号 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18880901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:37:15  来源:igfitidea点击:

Nested quotes bash

stringbashshellcommandnested

提问by BDR

I want to nest multiple strings like this :

我想像这样嵌套多个字符串:

sudo ssh server "awk "/pattern/{print "hello"}1" file > file.tmp"

With 2 nested quotes I managed to make my command works :

使用 2 个嵌套引号,我设法使我的命令有效:

awk "/pattern/{print \"hello\"}1" file > file.tmp

I cannot use single quote (') because there are variables in my command. Can someone help me ?

我不能使用单引号 ('),因为我的命令中有变量。有人能帮我吗 ?

Thanks in advance.

提前致谢。

回答by konsolebox

You can still place single quotes as long as the variables are intended to be initially expanded before the whole command string is executed on the shell on the remote server.

只要在远程服务器上的 shell 上执行整个命令字符串之前最初打算扩展变量,您仍然可以放置单引号。

sudo ssh server "echo \"$SOMEVAR\"; awk '/pattern/{print \"hello\"}1' file > file.tmp"