比方说,你有一个Bash别名:
alias rxvt='urxvt'
这很好。
然而:
alias rxvt='urxvt -fg '#111111' -bg '#111111''
不管用,也不管用:
alias rxvt='urxvt -fg \'#111111\' -bg \'#111111\''
那么,一旦有转义引号,如何在字符串中匹配开始和结束引号呢?
alias rxvt='urxvt -fg'\''#111111'\'' -bg '\''#111111'\''
看起来很笨拙,但如果允许像这样连接它们,它将表示相同的字符串。
恕我直言,真正的答案是你不能在单引号字符串中转义单引号。
它是不可能的。
如果我们假设使用bash。
从bash手册…
Enclosing characters in single quotes preserves the literal value of each
character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.
您需要使用其他字符串转义机制之一"或\
没有什么神奇的关于别名要求它使用单引号。
以下两种方法都可以在bash中工作。
alias rxvt="urxvt -fg '#111111' -bg '#111111'"
alias rxvt=urxvt\ -fg\ \'#111111\'\ -bg\ \'#111111\'
后者使用\来转义空格字符。
#111111要求单引号也没有什么神奇之处。
下面的选项实现了与其他两个选项相同的结果,因为rxvt别名按预期工作。
alias rxvt='urxvt -fg "#111111" -bg "#111111"'
alias rxvt="urxvt -fg \"#111111\" -bg \"#111111\""
您还可以直接逃避麻烦的#
alias rxvt="urxvt -fg \#111111 -bg \#111111"
除了@JasonWoof的完美答案,我想展示我是如何解决相关问题的
在我的例子中,用“\”编码单引号并不总是足够的,例如,如果字符串必须用单引号引用,但是引号的总数导致奇数
#!/bin/bash
# no closing quote
string='alecxs\'solution'
# this works for string
string="alecxs'solution"
string=alecxs\'solution
string='alecxs'\''solution'
让我们假设字符串是一个文件名,我们需要在一个列表中保存引用的文件名(如stat -c%N ./* > list)
echo "'$string'" > "$string"
cat "$string"
但是处理这个列表将失败(取决于字符串总共包含多少引号)
while read file
do
ls -l "$file"
eval ls -l "$file"
done < "$string"
解决方法:用字符串操作编码引号
string="${string//$'\047'/\'\$\'\\\\047\'\'}"
# result
echo "$string"
现在它起作用了,因为报价总是平衡的
echo "'$string'" > list
while read file
do
ls -l "$file"
eval ls -l "$file"
done < list
希望在遇到类似问题时能有所帮助
以下是上面提到的唯一正确答案的详细说明:
有时我会在ssh上使用rsync下载,并且必须转义一个带有'两次!(天啊!)一次用于bash,一次用于ssh。这里也使用了相同的交替引用分隔符原则。
例如,假设我们想要:Louis Theroux的LA Stories……
首先,将Louis Theroux用单引号括起来表示bash,用双引号括起来表示ssh:
“路易泰鲁”的
然后使用单引号转义双引号" "
使用双引号来转义撇号"'"
然后重复#2,使用单引号转义双引号“”
然后将LA Stories包含在bash的单引号中,ssh的双引号中:' ' LA Stories ' '
看哪!你会得到这样的结果:
rsync -ave ssh '"Louis Theroux"''"'"'"'"''"s LA Stories"'
对一个人来说,这是一件可怕的事情——但你明白了吗
shell_escape () {
printf '%s' "'${1//\'/\'\\\'\'}'"
}
实现说明:
双引号,这样我们可以很容易地输出换行单引号,并使用${…}语法
Bash的搜索和替换如下:${varname//search/replacement}
我们用“\”代替“\”
'\ "编码单个',如下所示:
单引号结束
\'编码a '(需要反斜杠,因为我们不在引号内)
又开始了单引号
Bash自动连接字符串,中间没有空格
在每个\和`之前都有一个\,因为这是${…//…/…的转义规则。}。
string="That's "'#@$*&^`(@#'
echo "original: $string"
echo "encoded: $(shell_escape "$string")"
echo "expanded: $(bash -c "echo $(shell_escape "$string")")"
注:始终编码为单引号字符串,因为它们比双引号字符串简单得多。