如何逃脱单引号字符串中的单引号? [英] How to escape single-quotes within single-quoted strings?

查看:153
本文介绍了如何逃脱单引号字符串中的单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,你有一个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 ':


  1. 结束它使用单引号第一个报价。

  2. 开始第二次报价,使用双引号。

  3. 引用的性格。

  4. 结束第二次报价,使用双引号。

  5. 启动第三次报价,使用单引号。

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆