在bash中用单引号引起来的单引号转义
问题
我正在尝试自动创建kickstart文件,而我用来解析该文件的工具要求将kickstart作为字符串提供在单引号中.
I'm trying to create a kickstart file automatically, and the tool i'm using to parse it requires the kickstart to be supplied as a string in single quotes.
除使用awk的后安装脚本外,大多数情况都很好,因此单引号内的单引号.
Most of it is fine, except the postinstall script which uses awk, hence single quotes within single quotes.
我查看了其他一些帖子,并尝试将字符串转义,但无济于事.
I've looked at some of the other posts, and tried escaping the strings, to no avail.
我显然误解了转义的一些基本原理.
I've clearly mis-understood some fundamental principal of escaping.
代码
DATA='
GATEWAY=$(localcli network ip route ipv4 list | grep default | awk \'{print $3}\')
'
echo ${DATA}
所需的输出
这是我想看到的文字输出字符串.
This is the literal output string i'd like to see.
GATEWAY=$(localcli network ip route ipv4 list | grep default | awk '{print $3}')
任何帮助将不胜感激!
谢谢
马特
由于您不能在单引号引起来的字符中转义字符,因此必须在其之外进行操作:
Since you can't escape characters within the single-quoted string, you have to do it outside of it:
~$ echo 'abc'\''cde'
abc'cde
或者,使用双引号引起来的字符串(需要转义 $
)
Alternatively, use a double-quoted string (requires escaping $
)
DATA="
GATEWAY=\$(localcli network ip route ipv4 list | grep default | awk '{print \$3}')
"