python - -bash : !/usr/bin/env: 找不到事件

标签 python linux bash macos echo

<分区>

我在尝试将 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 with set +H

关于python - -bash : !/usr/bin/env: 找不到事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798737/

相关文章:

linux - 获取最后 2 个调用函数地址的代码

python - 修复列 "date_of_birth"中的空值违反了非空约束

python - LeetCode 509 : Fibonacci Number "int object not subscriptable"

Python:将点分配给道路shapefile

linux - 如何检查 Linux 的 32 位/64 位内核

arrays - 在 Debian 8.4 中创建数组变量时出错

python - Python中任意大小的字节 block 的高效FIFO队列

python - fsolve 对任何方程组都有效吗?

bash - 使用 Bash 从包名称中剥离版本

linux - 从一个目录执行 bash 脚本到另一个目录?