Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use multiline <<EOF in bash with assigning output to variable?

Let’s say that I have a code like:

python <<EOF
print("abc")
EOF

and I want to assign output to bash variable. Something like:

VAR=$(python -c 'print("abc")')

Of course real python code is more complicated than print(abc) and one-liner can’t be used here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So the qestion is how to assign output of first example to variable?

I tried some variations of

VAR=$(python <<EOF
print("abc")
EOF )

VAR=$(python) <<EOF
print("abc")
EOF

But this syntax is not correct

>Solution :

I do it adding the closing parenthesis in a new line. This works for me:

VAR=$(python <<EOF
print("abc")
EOF
)

Hope it helps

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading