1

I am executing a python script from my bash script. The python script outputs 4 lines to stdout. I would like to store the output of these 4 lines in 4 different variables in bash or one array with 4 elements.

When I run my python script byitself I see the 4 lines on stdout:

$ python my_script.py
line1
line2
line3
line4

In my bash script I've done this:

OUTPUT="$(python my_script.py)"

echo "${OUTPUT}"

readarray -t y <<<"$OUTPUT" 

After above when I do echo $y I only see output of first line (line1).

How can I use the output of python script as 4 variables or an array?

2
  • 2
    you don't need an intermediate variable: mapfile -t y < <(python my_script.py) is enough. Now, to output the content of the array y to stdout, one field per line: printf '%s\n' "${y[@]}" or to inspect the array: declare -p y. Commented Dec 22, 2016 at 13:56
  • @Anthony: Check out my logic using read for bash versions which do not support readarray or maple! Commented Dec 23, 2016 at 16:00

2 Answers 2

2

$y is the first element of the array. ${y[@]} will give you all of them.

echo "${y[@]}"
Sign up to request clarification or add additional context in comments.

2 Comments

How can I get second third etc elements of the array individually?
Yeah. "${y[1]}", "${y[2]}", etc.
-1

Do you have bash as the primary parser or have the bash shabang. This because sh doesn't have this nice array handling...

But if you have why not create the bash array when you run the python script by the array creation parentheses? And then use Johns way to print them.

OUTPUT=( $(python my_script.py) )
echo "len of OUTPUT=${#OUTPUT[@]} and the data is:" ${OUTPUT[@]}
echo "Second entry is=" ${OUTPUT[1]}

... ...

6 Comments

Why not? Because it's broken: your command is subject to pathname expansion and globbing!
Is it? Where is it broken and which expansion?
Word splitting: output=( $(echo hello world) ). Pathname expansion: output=( $(echo '*') ) (run from within a non-empty directory). There are lots of other counterexamples. The known ways to fix these issues is to fight against the shell: alter IFS and use set -f. But it's really retarded since mapfile/readarray will do it more efficiently; moreover, that's what the OP used, so why suggest something worse than what is originally used?
The original question was how to take the output from a defined python script. Not any free defined strings or paths so why do it more complicated then necessary and do comments outside the question.
You're not using the right tool for the job (mapfile/readarray and read are the right tool for the job), and what you're suggesting is inherently broken. There's nothing wrong with the fact that you suggest something wrong, as the internet is full of wrong and broken techniques regarding shell scripting and Bash, and you probably learned from them. But now you should go one step beyond what you already learned and understand why what you thought was correct is wrong, and change your habits accordingly!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.