<分区>
我在尝试将 shebang 写入 python 脚本时收到此奇怪的错误消息:
$ echo "#!/usr/bin/env python" > scripts/sandbox.py
-bash: !/usr/bin/env: event not found
$ echo "say what?" > scripts/sandbox.py
立即做同样的事情但不使用 shebang 行。这是什么行为,如何克服?
<分区>
我在尝试将 shebang 写入 python 脚本时收到此奇怪的错误消息:
$ echo "#!/usr/bin/env python" > scripts/sandbox.py
-bash: !/usr/bin/env: event not found
$ echo "say what?" > scripts/sandbox.py
立即做同样的事情但不使用 shebang 行。这是什么行为,如何克服?
最佳答案
!
是 bash 的特殊字符,用于引用之前的命令。它在双引号内展开。
为避免这种情况,请将它们括在单引号中:
echo '#!/usr/bin/env python' > scripts/sandbox.py
@mklement0在评论中漂亮地阐明了它:
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
关于python - -bash : !/usr/bin/env: 找不到事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798737/