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.pyat the bash command prompt, bash uses the shebang line to figure out what executable will run the script.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.)If you want to start another bash shell which runs your Python script, then use
bash -c ./demo.pyto executedemo.pyas a bash command rather than as a bash script. But you shouldn’t need to start another shell just to run a Python script.