bash 由于 UTF-8 BOM(字节顺序标记),找不到 Shebang 可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19065522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shebang executable not found because of UTF-8 BOM (Byte Order Mark)
提问by Sébastien
For some reason the shebang in one of my scripts does not work:
由于某种原因,我的其中一个脚本中的 shebang 不起作用:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print "Hello World"
When I execute this file, I get an error
当我执行这个文件时,我收到一个错误
% ./test.py
./test.py: 1: #!/usr/bin/env: not found
There is no problem with the content of my /usr/bin/
directory: both env
and python
are there, with correct execution rights.
我的/usr/bin/
目录的内容没有问题:env
和python
都在那里,具有正确的执行权限。
回答by Sébastien
The cause of the problem is that my file was encoded using UTF8 with BOM (Byte Order Mark).
问题的原因是我的文件是使用带有 BOM(字节顺序标记)的 UTF8编码的。
Removing the BOM, i.e. encoding the file using UTF8 without BOM solves the issue.
删除 BOM,即使用没有 BOM 的 UTF8 编码文件解决了这个问题。
NB: for Notepad++ users, "UTF8 without BOM" is also called (weirdly) "ANSI as UTF-8" in the editor.
注意:对于 Notepad++ 用户,“没有 BOM 的 UTF8”在编辑器中也被称为(奇怪的)“ANSI as UTF-8”。
回答by Bakuriu
This is due to how Unix and Linux handle the shebang. #!
must be the first two bytesin the file. If you have a BOM then this isn't true anymore hence the error.
这是由于 Unix 和 Linux 处理 shebang 的方式。#!
必须是文件中的前两个字节。如果您有 BOM,那么这不再是正确的,因此会出现错误。
Note that putting a BOM is completely useless from the point of view of the python interpreter, since the # -*- coding: utf-8 -*-
already tells python the encoding.
请注意,从 python 解释器的角度来看,放置 BOM 是完全没用的,因为它# -*- coding: utf-8 -*-
已经告诉 python 编码。
AFAIK BOM is usually notused with utf-8. It is used for UTF-16 et similia in order to specify the byte-order. If the editor assumes the wrong encoding you should be able to explicitly open the file with the correct encoding.
AFAIK BOM 通常不与 utf-8 一起使用。它用于 UTF-16 et similia 以指定字节顺序。如果编辑器采用错误的编码,您应该能够使用正确的编码显式打开文件。