Saturday, October 29, 2022

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

Issue

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

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



Answered By - dubafek
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.