This dual-mode script is both a Posix shell script and a python script. The shell part looks like a triple-quoted string to the Python interpreter. The shell does not reach anything after the exec statement.
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python -E print "Nope, this doesn't work" #!/bin/sh LOADER=''''; exec python -E "$0" "$@" #''' print "This Python script is also a shell script!"
In this example, the -E argument is passed to the interpreter in order to prevent environment variables like PYTHONPATH from affecting the script. If you try to add this option on the standard #! header line this will not work - it will search for an executable named "python -E" and fail.
This trick can also have other uses. For example, it can search for an appropriate python version to run in different environments where "python" can be either python2 or python3. The triple-quoted string may be safely extended to a multiline script.