-bash:!/ usr / bin / env:找不到事件 [英] -bash: !/usr/bin/env: event not found
问题描述
我收到一条奇怪的错误消息,试图将shebang写入python脚本:
I got this strange error message trying to write shebang to a python script:
$ echo "#!/usr/bin/env python" > scripts/sandbox.py
-bash: !/usr/bin/env: event not found
$ echo "say what?" > scripts/sandbox.py
立即执行相同的操作,但不使用shebang行。这是什么行为,如何克服?谢谢
Immediately, doing the same thing but without using shebang line works. What is this behavior and how can it be overcome? thank you
推荐答案
!
是bash的特殊字符,它是用于引用先前的命令。它在双引号内扩展。
为避免这种情况,请用单引号引起来:
!
is a special character to bash, it is used to refer to previous commands. It is expanded within double-quotes.
To avoid that, enclose them in single-quotes instead:
echo '#!/usr/bin/env python' > scripts/sandbox.py
@ mklement0 在一条评论中对其进行了精美的说明:
@mklement0 clarified it beautifully in a comment:
更具体地说,是
!
是Bash的历史记录扩展功能的特殊功能,该功能默认情况下(仅)在交互式shell中处于启用状态。除了通过使用单引号引起来的字符串来避免它外,还可以与set + H
More specifically,
!
is special to Bash's history expansion feature, which is on by default (only) in interactive shells. In addition to avoiding it by using single-quoted strings, it can be turned off altogether withset +H
这篇关于-bash:!/ usr / bin / env:找不到事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!