How to execute multiline python code from a bash script?
<p>I need to extend a shell script (bash). As I am much more familiar with python I want to do this by writing some lines of python code which depends on variables from the shell script. Adding an extra python file is not an option.</p>

<pre><code>result=`python -c "import stuff; print('all $code in one very long line')"` 
</code></pre>

<p>is not very readable.</p>

<p>I would prefer to specify my python code as a multiline string and then execute it.</p>

<p>Tanks to <a href="http://stackoverflow.com/a/37222377/333403">this SO answer</a> I found the answer myself:</p>

<pre><code>#!/bin/bash

# some bash code
END_VALUE=10

PYTHON_CODE=$(cat &lt;&lt;END
# python code starts here
import math

for i in range($END_VALUE):
    print(i, math.sqrt(i))

# python code ends here
END
)

# use the 
res="$(python3 -c "$PYTHON_CODE")"

# continue with bash code
echo "$res"
</code></pre>

python bash multiline