如何在单引号引起来的字符串中转义单引号
比方说,您有一个Bash alias
,例如:
Let's say, you have a Bash alias
like:
alias rxvt='urxvt'
效果很好.
但是:
alias rxvt='urxvt -fg '#111111' -bg '#111111''
将不起作用,也将不会:
won't work, and neither will:
alias rxvt='urxvt -fg \'#111111\' -bg \'#111111\''
那么一旦转义了引号,您如何最终匹配字符串中的开始和结束引号?
So how do you end up matching up opening and closing quotes inside a string once you have escaped quotes?
alias rxvt='urxvt -fg'\''#111111'\'' -bg '\''#111111'\''
似乎难以理解,尽管如果允许您将它们串联在一起的话,它将表示相同的字符串.
seems ungainly although it would represent the same string if you're allowed to concatenate them like that.
如果您确实要在最外层使用单引号,请记住可以同时粘贴两种引号.示例:
If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:
alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234
'"'"'
如何解释为'
的解释:
-
'
使用单引号结束第一个引号. -
"
使用双引号开始第二个报价. -
'
引号字符. -
"
使用双引号结束第二个报价. -
'
使用单引号将第三个引号引起来.
-
'
End first quotation which uses single quotes. -
"
Start second quotation, using double-quotes. -
'
Quoted character. -
"
End second quotation, using double-quotes. -
'
Start third quotation, using single quotes.
如果您没有在(1)与(2)之间或(4)与(5)之间放置任何空格,则Shell会将该字符串解释为一个长字.
If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.