0

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

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • 2
    Place the closing `)` after the line or below the closing `EOF`, on a side note you can quote the opening `EOF` e.g. `'EOF'` so special characters from the shell will not be expanded/interpeted – Jetchisel Oct 05 '22 at 02:14
  • 1
    See `PAGER='less +/^[[:space:]]*Here\ Documents' man bash` – Jetchisel Oct 05 '22 at 02:20

1 Answers1

0

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

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

Hope it helps

dubafek
  • 1,073
  • 9
  • 21