由于UTF-8 BOM(字节顺序标记)而未找到Shebang可执行文件 [英] Shebang executable not found because of UTF-8 BOM (Byte Order Mark)

查看:76
本文介绍了由于UTF-8 BOM(字节顺序标记)而未找到Shebang可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我的一个脚本中的shebang无效:

For some reason the shebang in one of my scripts does not work:

#!/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

我的/usr/bin/目录的内容没有问题: env python 都存在,并且执行正确权利.

There is no problem with the content of my /usr/bin/ directory: both env and python are there, with correct execution rights.

推荐答案

这是由于Unix和Linux如何处理shebang. #! 必须是前两个字节.如果您有BOM,则不再适用,因此会出现错误.

This is due to how Unix and Linux handle the shebang. #! must be the first two bytes in the file. If you have a BOM then this isn't true anymore hence the error.

请注意,从python解释器的角度来看,放置BOM完全没有用,因为#-*-编码:utf-8-*-已经告诉了python编码.

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.

AFAIK BOM表通常与utf-8一起使用.它用于UTF-16等以指定字节顺序.如果编辑器假定编码错误,则应该可以使用正确的编码显式打开文件.

AFAIK BOM is usually not used 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.

这篇关于由于UTF-8 BOM(字节顺序标记)而未找到Shebang可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆