"""foo""" (text inside of triple quotes) is often treated as a comment
in Python. So the entire line up until the # is treated as a comment
by Python and is ignored when Python reads the script file. Since #
until LF is also a comment in Python, the remainder of the line is
also ignored by Python, so the entire line is ignored by Python.
"" in Bourne shell is an empty string. So the first "" in """ at the
start of the line expands to an empty string, which is then
concatenated with the next double quoted string, "exec". Since ""
plus "exec" is still "exec", Bourne reads this as though it had just
said exec right there.
At the end of the line again the first "" in """ is read as an empty
string, which is then concatenated with "#$magic". The shell expands
$magic to the value defined on the prior line, effectively adding this
to the argument vector given to Python when it execs Python. So in
the end this line looks to the Bourne shell as though it were:
exec python -E "$0" "$@" "#--calling-python-from-/bin/sh--"
Other scripting languages like Perl and Tcl have similar voodoo used
to make a script executable by both a Bourne shell and also by the
real language engine. Perl needs a less obtuse trick, IIRC its as
simple as:
#!/bin/sh
exec 'perl' "$@"
if 0;
and Tcl can use:
#!/bin/sh
# Tcl ignores the next line -*- tcl -*- \
exec tcl "$@"
Python proved much more difficult to get this to work. So the code
snippet is a bit more complex.