比方说,你有一个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'\''

看起来很笨拙,但如果允许像这样连接它们,它将表示相同的字符串。


当前回答

如何逃脱单引号(')和双引号(")与十六进制和八进制字符

如果使用像echo这样的东西,我有一些非常复杂,非常奇怪和难以逃避(想想:非常嵌套)的情况下,我唯一能做的就是使用八进制或十六进制代码!

下面是一些基本的例子来演示它是如何工作的:

1. 单引号示例,其中'转义为十六进制\x27或八进制\047(其对应的ASCII码):

十六进制\ x27 echo -e“让\x27s开始编码!” #或 echo -e“让\x27s开始编码!” 结果: 让我们开始编码吧! 八进制\ 047 echo -e“让047s开始编码!” #或 echo -e“让047s开始编码!” 结果: 让我们开始编码吧!

2. 双引号示例,其中"转义为十六进制\x22或八进制\042(其对应的ASCII码)。

注意:bash太疯狂了!有时甚至!Char有特殊的含义,必须从双引号内删除,然后转义为“像这样”\!或者完全用单引号括起来,像这样!,而不是在双引号内。

# 1. hex; also escape `!` by removing it from within the double quotes 
# and escaping it with `\!`
$ echo -e "She said, \x22Let\x27s get coding"\!"\x22"
She said, "Let's get coding!"

# OR put it all within single quotes:
$ echo -e 'She said, \x22Let\x27s get coding!\x22'
She said, "Let's get coding!"


# 2. octal; also escape `!` by removing it from within the double quotes 
$ echo -e "She said, \042Let\047s get coding"\!"\042"
She said, "Let's get coding!"

# OR put it all within single quotes:
$ echo -e 'She said, \042Let\047s get coding!\042'
She said, "Let's get coding!"


# 3. mixed hex and octal, just for fun
# also escape `!` by removing it from within the double quotes when it is followed by
# another escape sequence
$ echo -e "She said, \x22Let\047s get coding! It\x27s waaay past time to begin"\!"\042"
She said, "Let's get coding! It's waaay past time to begin!"

# OR put it all within single quotes:
$ echo -e 'She said, \x22Let\047s get coding! It\x27s waaay past time to begin!\042'
She said, "Let's get coding! It's waaay past time to begin!"

注意,如果你在需要的时候没有正确地转义!,就像我上面展示的两种方法一样,你会得到一些奇怪的错误,像这样:

$ echo -e "She said, \x22Let\047s get coding! It\x27s waaay past time to begin!\042"
bash: !\042: event not found

OR:

$ echo -e "She said, \x22Let\x27s get coding!\x22"
bash: !\x22: event not found

另一种选择:这允许在同一个bash字符串中混合展开和非展开

这里是另一个替代逃逸技术的演示。

首先,阅读@liori的主要回答,看看下面的第二种形式是如何工作的。现在,阅读这两种转义字符的方法。下面两个例子的输出是相同的:

CMD="gs_set_title"

# 1. 1st technique: escape the $ symbol with a backslash (\) so it doesn't 
# run and expand the command following it
echo "$CMD '\$(basename \"\$(pwd)\")'"

# 2. 2nd technique (does the same thing in a different way): escape the 
# $ symbol using single quotes around it, and the single quote (') symbol
# using double quotes around it
echo "$CMD ""'"'$(basename "$(pwd)")'"'"

样例输出:

Gs_set_title '$(basename "$(pwd)")' Gs_set_title '$(basename "$(pwd)")'

注意:对于我的gs_set_title bash函数,它在我的~/。Bash_aliases文件在这里,看到我的另一个答案。

引用:

https://en.wikipedia.org/wiki/ASCII#Printable_characters https://serverfault.com/questions/208265/what-is-bash-event-not-found/208266#208266 请参见我在这里的另一个回答:如何使用echo编写非ascii字符?

其他回答

如果你已经安装了GNU Parallel,你可以使用它的内部引用:

$ parallel --shellquote
L's 12" record
<Ctrl-D>
'L'"'"'s 12" record'
$ echo 'L'"'"'s 12" record'
L's 12" record

从版本20190222,你甚至可以——shellquote多次:

$ parallel --shellquote --shellquote --shellquote
L's 12" record
<Ctrl-D>
'"'"'"'"'"'"'L'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'s 12" record'"'"'"'"'"'"'
$ eval eval echo '"'"'"'"'"'"'L'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'s 12" record'"'"'"'"'"'"'
L's 12" record

它将在所有受支持的shell(不仅仅是bash)中引用该字符串。

在给定的例子中,简单地使用双引号而不是单引号作为外部转义机制:

alias rxvt="urxvt -fg '#111111' -bg '#111111'"

这种方法适用于您只想将固定字符串传递给命令的许多情况:只需检查shell如何通过echo解释双引号字符串,并在必要时使用反斜杠转义字符。

在这个例子中,你会看到双引号足以保护字符串:

$ echo "urxvt -fg '#111111' -bg '#111111'"
urxvt -fg '#111111' -bg '#111111'

如何逃脱单引号(')和双引号(")与十六进制和八进制字符

如果使用像echo这样的东西,我有一些非常复杂,非常奇怪和难以逃避(想想:非常嵌套)的情况下,我唯一能做的就是使用八进制或十六进制代码!

下面是一些基本的例子来演示它是如何工作的:

1. 单引号示例,其中'转义为十六进制\x27或八进制\047(其对应的ASCII码):

十六进制\ x27 echo -e“让\x27s开始编码!” #或 echo -e“让\x27s开始编码!” 结果: 让我们开始编码吧! 八进制\ 047 echo -e“让047s开始编码!” #或 echo -e“让047s开始编码!” 结果: 让我们开始编码吧!

2. 双引号示例,其中"转义为十六进制\x22或八进制\042(其对应的ASCII码)。

注意:bash太疯狂了!有时甚至!Char有特殊的含义,必须从双引号内删除,然后转义为“像这样”\!或者完全用单引号括起来,像这样!,而不是在双引号内。

# 1. hex; also escape `!` by removing it from within the double quotes 
# and escaping it with `\!`
$ echo -e "She said, \x22Let\x27s get coding"\!"\x22"
She said, "Let's get coding!"

# OR put it all within single quotes:
$ echo -e 'She said, \x22Let\x27s get coding!\x22'
She said, "Let's get coding!"


# 2. octal; also escape `!` by removing it from within the double quotes 
$ echo -e "She said, \042Let\047s get coding"\!"\042"
She said, "Let's get coding!"

# OR put it all within single quotes:
$ echo -e 'She said, \042Let\047s get coding!\042'
She said, "Let's get coding!"


# 3. mixed hex and octal, just for fun
# also escape `!` by removing it from within the double quotes when it is followed by
# another escape sequence
$ echo -e "She said, \x22Let\047s get coding! It\x27s waaay past time to begin"\!"\042"
She said, "Let's get coding! It's waaay past time to begin!"

# OR put it all within single quotes:
$ echo -e 'She said, \x22Let\047s get coding! It\x27s waaay past time to begin!\042'
She said, "Let's get coding! It's waaay past time to begin!"

注意,如果你在需要的时候没有正确地转义!,就像我上面展示的两种方法一样,你会得到一些奇怪的错误,像这样:

$ echo -e "She said, \x22Let\047s get coding! It\x27s waaay past time to begin!\042"
bash: !\042: event not found

OR:

$ echo -e "She said, \x22Let\x27s get coding!\x22"
bash: !\x22: event not found

另一种选择:这允许在同一个bash字符串中混合展开和非展开

这里是另一个替代逃逸技术的演示。

首先,阅读@liori的主要回答,看看下面的第二种形式是如何工作的。现在,阅读这两种转义字符的方法。下面两个例子的输出是相同的:

CMD="gs_set_title"

# 1. 1st technique: escape the $ symbol with a backslash (\) so it doesn't 
# run and expand the command following it
echo "$CMD '\$(basename \"\$(pwd)\")'"

# 2. 2nd technique (does the same thing in a different way): escape the 
# $ symbol using single quotes around it, and the single quote (') symbol
# using double quotes around it
echo "$CMD ""'"'$(basename "$(pwd)")'"'"

样例输出:

Gs_set_title '$(basename "$(pwd)")' Gs_set_title '$(basename "$(pwd)")'

注意:对于我的gs_set_title bash函数,它在我的~/。Bash_aliases文件在这里,看到我的另一个答案。

引用:

https://en.wikipedia.org/wiki/ASCII#Printable_characters https://serverfault.com/questions/208265/what-is-bash-event-not-found/208266#208266 请参见我在这里的另一个回答:如何使用echo编写非ascii字符?

恕我直言,真正的答案是你不能在单引号字符串中转义单引号。

它是不可能的。

如果我们假设使用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"

由于Bash 2.04语法$'string'允许一个转义限制集。

自Bash 4.4以来,$'string'还允许完整的c风格转义,使得$'string'的行为在以前的版本中略有不同。(以前可以使用$('string')形式。)

Bash 2.04及更新版本中的简单示例:

  $> echo $'aa\'bb'
  aa'bb

  $> alias myvar=$'aa\'bb'
  $> alias myvar
  alias myvar='aa'\''bb'

在你的情况下:

$> alias rxvt=$'urxvt -fg \'#111111\' -bg \'#111111\''
$> alias rxvt
alias rxvt='urxvt -fg '\''#111111'\'' -bg '\''#111111'\'''

常见的转义序列按预期工作:

\'     single quote
\"     double quote
\\     backslash
\n     new line
\t     horizontal tab
\r     carriage return

下面是从man bash(4.4版)复制粘贴相关文档:

形式为$'string'的单词被特殊处理。单词扩展为字符串,反斜杠转义字符替换为ANSI C标准指定的字符。如果存在反斜杠转义序列,则按如下方式解码:

    \a     alert (bell)
    \b     backspace
    \e
    \E     an escape character
    \f     form feed
    \n     new line
    \r     carriage return
    \t     horizontal tab
    \v     vertical tab
    \\     backslash
    \'     single quote
    \"     double quote
    \?     question mark
    \nnn   the eight-bit character whose value is the octal 
           value nnn (one to three digits)
    \xHH   the eight-bit character whose value is the hexadecimal
           value HH (one or two hex digits)
    \uHHHH the Unicode (ISO/IEC 10646) character whose value is 
           the hexadecimal value HHHH (one to four hex digits)
    \UHHHHHHHH the Unicode (ISO/IEC 10646) character whose value 
               is the hexadecimal value HHHHHHHH (one to eight 
               hex digits)
    \cx    a control-x character

展开的结果是单引号,就好像美元符号不存在一样。


更多细节请参见bash.hackers.org wiki上的引用和转义:ANSI C like字符串。还要注意,“Bash Changes”文件(概述在这里)提到了很多与$'string'引用机制相关的更改和错误修复。

如何将一个特殊字符作为一个普通字符使用?它应该工作(与一些变化)在bash, zsh, mksh, ksh93和FreeBSD和busybox sh。