如何在单引号引起来的字符串中转义单引号 [英] How to escape single quotes within single quoted strings

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

问题描述

比方说,您有一个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

'"'"'如何解释为'的解释:

  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)之间放置任何空格,则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.

这篇关于如何在单引号引起来的字符串中转义单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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