#!/usr/bin/python(sha-bang 或 she-bang)在 bash 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9269969/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:33:38  来源:igfitidea点击:

#!/usr/bin/python (sha-bang or she-bang)does not work in bash

pythonbashubuntu

提问by Talespin_Kit

I have the following contents in the file

我在文件中有以下内容

demo.py:- // executable bit set

demo.py:- // 可执行位设置

#!/usr/bin/python
import os

i have used the command bash demo.pyin the terminal and expecting that the first line is interpreted by the bash and it handles the file to python interpreter. But it calls the binary "/usr/bin/import"(figured using strace). The same is with sh demo.py. However running ./demo.pyworks. man bash says

我在终端中使用了命令bash demo.py并期望第一行由 bash 解释并将文件处理到 python 解释器。但它调用二进制文件“/usr/bin/import”(使用strace计算)。sh demo.py也是如此。但是运行./demo.py 是有效的。人 bash 说

"If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program."

“如果程序是以#! 开头的文件,则第一行的其余部分指定程序的解释器。”

which is not happening.

这没有发生。

Using bash version

使用 bash 版本

$ bash --version

$ bash --version

GNU bash, version 4.2.8(1)-release (i686-pc-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

GNU bash,版本 4.2.8(1)-release (i686-pc-linux-gnu) 版权所有 (C) 2011 Free Software Foundation, Inc. 许可证 GPLv3+:GNU GPL 版本 3 或更高版本http://gnu.org/licenses /gpl.html

This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

这是免费软件;您可以自由更改和重新分发它。在法律允许的范围内,不提供任何保证。

in Ubuntu 11.04

在 Ubuntu 11.04 中

回答by kindall

The bash documentation is correct: when you enter ./demo.pyat the bash command prompt, bash uses the shebang line to figure out what executable will run the script.

bash 文档是正确的:当您./demo.py在 bash 命令提示符下输入时,bash 使用 shebang 行来确定哪个可执行文件将运行脚本。

When you do bash demo.pythen of course bash is going to try to run it as a bash script. Because you told it to. (Imagine if you had a bash script with an incorrect shebang line -- how would you run it? By passing it directly to bash, in just this way.)

当你这样做时bash demo.py,当然 bash 会尝试将它作为 bash 脚本运行。因为你告诉了它。(想象一下,如果您有一个带有不正确 shebang 行的 bash 脚本——您将如何运行它?通过这种方式将它直接传递给 bash。)

If you want to start another bash shell which runs your Python script, then use bash -c ./demo.pyto execute demo.pyas a bash commandrather than as a bash script. But you shouldn't need to start another shell just to run a Python script.

如果您想启动另一个运行 Python 脚本的 bash shell,则使用bash -c ./demo.pydemo.py作为 bash 命令而不是作为 bash 脚本执行。但是您不需要为了运行 Python 脚本而启动另一个 shell。

回答by jcollado

I think you're confused with the meaning of the she-bang line.

我认为您对 she-bang 线的含义感到困惑。

#!/usr/bin/python

This means that ./demo.pywill execute as /usr/bin/python demo.py.

这意味着./demo.py将作为/usr/bin/python demo.py.

However, with /bin/bash demo.py, bashwill try to interpret demo.pyas a shell script file and will fail, that is, pythonwon't be executed.

但是,使用/bin/bash demo.py,bash将尝试解释demo.py为 shell 脚本文件并且会失败,即python不会被执行。

回答by Andrew Clark

You need to run it as bash -c ./demo.pyor sh -c ./demo.py, otherwise each line from the file will be executed as a bash command (rather than executing the file using the she-bang).

您需要将其作为bash -c ./demo.pyor运行sh -c ./demo.py,否则文件中的每一行都将作为 bash 命令执行(而不是使用 she-bang 执行文件)。

回答by user2570248

BTW, beware of cut'n'paste from Windows web or documents, CR-LF after shebang does not work well:

顺便说一句,当心从 Windows 网络或文档中剪切“n”粘贴,shebang 后的 CR-LF 无法正常工作:

=> head -1 myScript.py | od -cx

=> head -1 myScript.py | od -cx

0000000 # ! / u s r / b i n / e n v p

0000000#!/usr/bin/envp

       2123    752f    7273    622f    6e69    652f    766e    7020

0000020 y t h o n \r \n

0000020 ython \r \n

       7479    6f68    0d6e    000a

0000027

0000027