make #!/usr/bin/env python
This question already has an answer here:
I'd use the following:
#!/bin/sh
"""true"
exec python -u "$0" "$@"
"""
# python code goes here
The line """true"
will be parsed by sh
as true
, because it consists of an empty ""
string followed by "true"
. Since true
is a no-op command, it will be effectively ignored, and the following line will execute the Python interpreter.
On the other hand, Python will parse the """true"
line very differently, as the opening of a triple-quoted string which starts with true"
and is closed two lines below. Since the string is not used for anything, the Python interpreter will effectively ignore the shell snippet that starts up Python. It is the difference in interpretation of """xxx"
that allows Python and sh
code coexist in the same script.
For a simple test, append something like:
import sys
print "hello!", sys.argv
Given a reasonable sh
implementation (and taking into account the time to start Python), this should not be measurably slower than using env
.