Mixed shell and python script possible?

You can use this shell syntax (it is called here document in Unix literature):

#!/bin/sh
echo this is a shell script

python <<@@
print 'hello from Python!'
@@

The marker after '<<' operator can by an arbitrary identifier, people often use something like EOF (end of file) or EOD (end of document). If the marker starts a line then the shell interprets it as end of input for the program.


If your python script is very short. You can pass it as a string to python using the -c option:

python -c 'import sys; print "xyzzy"; sys.exit(0)'

Or

python -c '
import sys
print("xyzzy")
sys.exit(0)
'

You could write

exec python <<END_OF_PYTHON

import sys

print ("xyzzy")

sys.exit(0)
END_OF_PYTHON

to replace the Bash process with Python and pass the specified program to Python on its standard input. (The exec replaces the Bash process. The <<END_OF_PYTHON causes standard input to contain everything up till END_OF_PYTHON.)