Exclamation mark inside double quotes results in a strange parse error [duplicate]

微笑、不失礼 提交于 2019-11-28 09:33:04

问题


Why does this command line work:

$ 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

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?

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

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

回答1:


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

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.

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

!mysql

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

!?grep

The bash manual also documents the syntax of the history expansion operators.



来源:https://stackoverflow.com/questions/12887906/exclamation-mark-inside-double-quotes-results-in-a-strange-parse-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!