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

参见英文答案 > Strange behavior of argv when passing string containing “!!!!”3个
为什么这个命令行有效:
$output='Irrelevant'; if [[ $output =~ Something ]]; then echo "I found something in the output." ; fi

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

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

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

如果重要,这是bash –version的输出

GNU bash,version 4.2.24(1)-release (x86_64-pc-linux-gnu)
您可以将字符串换成单引号而不是双引号.

感叹号调用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开头的最后一个命令,请输入:

!MysqL

或者执行包含单词grep的最后一个命令,输入:

!?grep

bash手册还记录了history expansion operators的语法.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...