pythonbashmultiline

How can I execute multiline Python code from a Bash script?


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.

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

is not very readable.

I would prefer to specify my Python code as a multiline string and then execute it.


Solution

  • Use a here-doc:

    result=$(python <<EOF
    import stuff
    print('all $code in one very long line')
    EOF
    )