如何逃脱Bash的单引号字符串中的单引号? [英] How to escape a single quote in single quote string in Bash?

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

问题描述

我要显示这样的Bash的字符串

 我是学生

当然,你可以做这样的

 回声我是学生

但如何实现使用字符串周围单引号这一点的同时?


解决方案

 呼应'我\\'米的学生

不起作用。但以下工作:

 回声$'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.

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

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