双引号内的感叹号导致奇怪的解析错误 [英] Exclamation mark inside double quotes results in a strange parse error

查看:227
本文介绍了双引号内的感叹号导致奇怪的解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此命令行为何起作用:

$ output='Irrelevant'; if [[ $output =~ Something ]]; then echo "I found something in the output." ; fi

这给了我一个奇怪的解析错误?

And this one give me a strange parse error?

$ output='Irrelevant'; if [[ $output =~ Something ]]; then echo "I found something in the output!" ; fi
-bash: !": event not found

与第一个版本相比,仅 的变化是引号内要回显的句子以感叹号结尾. 为什么Bash在第二个版本中给我这个错误?

The only change from the first version is that the sentence to be echoed inside quotes ends with an exclamation mark. Why does Bash give me that error in the second version?

这很重要,这是bash --version的输出:

In case it matters, this is the output from bash --version:

GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)

推荐答案

您可以将字符串用单引号引起来,而不用双引号引起来.

You can wrap the string in single quotes instead of double quotes.

感叹号调用 bash手册.

The exclamation point invokes the very useful history expansion function described in the bash manual.

历史扩展由历史扩展字符的外观引入,默认情况下为!.只能使用\'来转义历史扩展字符.

History expansions are introduced by the appearance of the history expansion character, which is ! by default. Only \ and ' may be used to escape the history expansion character.

例如,要执行以mysql开头的最后一个命令,请输入以下内容:

For instance, to execute the last command that started with the word mysql type this:

!mysql

或要执行包含单词grep的最后一条命令,请键入以下内容:

or to execute the last command containing the word grep, type this:

!?grep

bash手册还记录了历史记录的语法扩展运算符.

这篇关于双引号内的感叹号导致奇怪的解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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