#!/usr/bin/python (sha

I have the following contents in the file

demo.py:- // executable bit set

#!/usr/bin/python
import os

i have used the command bash demo.py in 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.py works. man bash says

"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 --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

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


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

When you do bash demo.py then 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.)

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


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

#!/usr/bin/python

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

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


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

链接地址: http://www.djcxy.com/p/19068.html

上一篇: 使用标准的linux命令进行分段错误

下一篇: #!/ usr / bin / python(sha