双引号内的感叹号会导致奇怪的解析错误

Del*_*ted 3 bash quotes

为什么这个命令行有效:

$ output='Irrelevant'; if [[ $output =~ Something ]]; then echo "I found something in the output." ; fi
Run Code Online (Sandbox Code Playgroud)

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

$ output='Irrelevant'; if [[ $output =~ Something ]]; then echo "I found something in the output!" ; fi
-bash: !": event not found
Run Code Online (Sandbox Code Playgroud)

从第一个版本的变化是引号内被呼应了一句带有感叹号结束.为什么Bash在第二个版本中给我错误?

如果重要,这是以下产出bash --version:

GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

jsp*_*cal 6

您可以将字符串换成单引号而不是双引号.

感叹号调用bash手册中描述的非常有用的历史扩展功能.

历史扩展是由历史扩展字符的外观引入的!,默认情况下.只有\'可以用来逃避历史扩展字符.

例如,要执行以word mysql类型开头的最后一个命令:

!mysql
Run Code Online (Sandbox Code Playgroud)

或者要执行包含该单词的最后一个命令grep,请键入:

!?grep
Run Code Online (Sandbox Code Playgroud)

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