微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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的语法.

原文地址:https://www.jb51.cc/bash/385199.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐