This answer discusses how to run a multi-line Python snippet from the command line in a terminal. I noticed that the answer works great within shell scripts, even with nested indentation, which is very nice, e.g.
#!/bin/bash
some_text="Hello world"
echo $some_text
cat <<EOF | python -
import sys;
for r in range(3):
print r
for a in range(2):
print "hello"
EOF
prints:
0
hello
hello
1
hello
hello
2
hello
hello
However, I am having a hard time sharing variables between the shell script and the Python snippet.
How can I collect the output of the python subscript in the bash script? (e.g. in a variable such as
$output
).How can I pass a bash variable (e.g.
$some_text
) to the Python script?
python - <<EOF
instead. – Evan Teitelman Apr 30 at 22:06