如何逃脱单引号字符串中的单引号? [英] How to escape single-quotes within single-quoted strings?
问题描述
让我们说,你有一个bash 别名
这样的:
Let's say, you have a bash alias
like:
alias rxvt='urxvt'
这工作得很好。
不过:</ P>
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'\''
似乎笨拙,虽然它会重新present相同的字符串,如果你被允许将它们串联那样的。
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
说明如何是间preTED刚才
:
Explanation of how '"'"'
is interpreted as just '
:
-
-
开始第二次报价,使用双引号。
-
-
结束第二次报价,使用双引号。
-
'
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),外壳间preT的字符串作为一个长字。
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.
这篇关于如何逃脱单引号字符串中的单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!