比方说,你有一个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'\''
看起来很笨拙,但如果允许像这样连接它们,它将表示相同的字符串。
这个函数:
quote ()
{
local quoted=${1//\'/\'\\\'\'};
printf "'%s'" "$quoted"
}
允许引用“内部”。像这样使用:
$ quote "urxvt -fg '#111111' -bg '#111111'"
'urxvt -fg '\''#111111'\'' -bg '\''#111111'\'''
如果要引用的行变得更复杂,比如双引号和单引号混合在一起,那么在变量中获得要引用的字符串可能会变得相当棘手。当出现这种情况时,在脚本中编写需要引用的确切行(类似于此)。
#!/bin/bash
quote ()
{
local quoted=${1//\'/\'\\\'\'};
printf "'%s'" "$quoted"
}
while read line; do
quote "$line"
done <<-\_lines_to_quote_
urxvt -fg '#111111' -bg '#111111'
Louis Theroux's LA Stories
'single quote phrase' "double quote phrase"
_lines_to_quote_
将输出:
'urxvt -fg '\''#111111'\'' -bg '\''#111111'\'''
'Louis Theroux'\''s LA Stories'
''\''single quote phrase'\'' "double quote phrase"'
所有在单引号内正确引用的字符串。
我总是将每个嵌入的单引号替换为序列:'\ "(即:quote反斜杠quote quote),它关闭字符串,附加一个转义的单引号并重新打开字符串。
我经常在Perl脚本中创建一个“quotify”函数来为我做这件事。步骤如下:
s/'/'\\''/g # Handle each embedded quote
$_ = qq['$_']; # Surround result with single quotes.
这基本上解决了所有情况。
当您在shell脚本中引入eval时,生活会变得更加有趣。你必须重新对所有东西进行报价!
例如,创建一个名为quotify的Perl脚本,包含上面的语句:
#!/usr/bin/perl -pl
s/'/'\\''/g;
$_ = qq['$_'];
然后使用它生成一个正确引用的字符串:
$ quotify
urxvt -fg '#111111' -bg '#111111'
结果:
'urxvt -fg '\''#111111'\'' -bg '\''#111111'\'''
然后可以复制/粘贴到alias命令:
alias rxvt='urxvt -fg '\''#111111'\'' -bg '\''#111111'\'''
(如果需要在eval中插入该命令,请再次运行quotify:
$ quotify
alias rxvt='urxvt -fg '\''#111111'\'' -bg '\''#111111'\'''
结果:
'alias rxvt='\''urxvt -fg '\''\'\'''\''#111111'\''\'\'''\'' -bg '\''\'\'''\''#111111'\''\'\'''\'''\'''
可以复制/粘贴到eval中:
eval 'alias rxvt='\''urxvt -fg '\''\'\'''\''#111111'\''\'\'''\'' -bg '\''\'\'''\''#111111'\''\'\'''\'''\'''
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")")"
注:始终编码为单引号字符串,因为它们比双引号字符串简单得多。