在bash中用单引号引起来的单引号转义 [英] single quote escaping in bash within single quoted string

查看:116
本文介绍了在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}')
"

这篇关于在bash中用单引号引起来的单引号转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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