且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何逃脱Bash的单引号字符串中的单引号?

更新时间:2022-12-10 23:32:41

 呼应'我\\'米的学生

不起作用。但以下工作:

 回声$'I \\'米的学生

从庆典的手册页:


  

单引号不能出现一个单引号,即使preceded
  一个反斜杠。结果
  ....结果
  形式的 $字符串'字被特殊处理。这个单词
  扩展为字符串,替换成反斜杠转义字符
  由ANSI C标准的规定。


块引用>

I want to display a string in Bash like this

I'm a student

Of course you can do it like this

echo "I'm a student"

But how to accomplish this while using single quote around the string ?

echo 'I\'m a student'

does not work. But the following works:

echo $'I\'m a student'

From the man page of bash:

A single quote may not occur between single quotes, even when preceded by a backslash.
....
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.