在bash中的单引号中使用单引号
Contents
1. 概述
*在bash *中引用非常简单。但是,有时,当我们添加一些限制时,它变得具有挑战性。其中一个限制是在单引号本身内转义单引号。
在本教程中,我们将讨论实现此目的的一些方法。
2. 使用美元 ($) 符号
**在bash中,以美元 ($) 符号开头的字符串会被特殊处理。**我们可以利用这个属性来转义单引号:
$ echo $'Problems aren\'t stop signs, they are guidelines'
Problems aren't stop signs, they are guidelines
请注意,字符串的开头有一个美元 ($) 符号。
3. 使用转义序列
另一种方法是转义单引号本身。让我们将给定的字符串分成三部分:
'Problems aren' + \' + 't stop signs, they are guidelines'
**在上面的示例中,加号 (+) 表示串联操作。**让我们删除加号和空格字符以达到所需的结果:
$ echo 'Problems aren'\''t stop signs, they are guidelines'
Problems aren't stop signs, they are guidelines
4. 使用单引号和双引号的组合
与前面的示例类似,我们可以使用单引号和双引号的组合:
"Problems aren" + "'" + "t stop signs, they are guidelines"
让我们删除加号和空格字符:
$ echo "Problems aren""'""t stop signs, they are guidelines"
Problems aren't stop signs, they are guidelines