Capture multiline output as array in bash

Bash

Bash Problem Overview


If inner.sh is

#...
echo first
echo second
echo third

And outer.sh is

var=`./inner.sh`
# only wants to use "first"...  

How can var be split by whitespace?

Bash Solutions


Solution 1 - Bash

Try this:

var=($(./inner.sh))

# And then test the array with:

echo ${var[0]}
echo ${var[1]}
echo ${var[2]}

Output:

first
second
third

Explanation:

  • You can make an array in bash by doing var=(first second third), for example.
  • $(./inner.sh) runs the inner.sh script, which prints out first, second, and third on separate lines. Since we don't didn't put double quotes around $(...), they get lumped onto the same line, but separated by spaces, so you end up with what it looks like in the previous bullet point.

Solution 2 - Bash

Don't forget the builtin mapfile. It's definitely the most efficient in your case: If you want to slurp the whole file in an array, the fields of which will be the lines output by ./inner.sh, do

mapfile -t array < <(./inner.sh)

Then you'll have the first row in ${array[0]}, etc...

For more info on this builtin and all the possible options:

help mapfile

If you just need the first line in a variable called firstline, do

read -r firstline < <(./inner.sh)

These are definitely the most efficient ways!

This small benchmark will prove it:

$ time mapfile -t array < <(for i in {0..100000}; do echo "fdjsakfjds$i"; done)

real	0m1.514s
user	0m1.492s
sys	0m0.560s
$ time array=( $(for i in {0..100000}; do echo "fdjsakfjds$i"; done) )

real	0m2.045s
user	0m1.844s
sys	0m0.308s

If you only want the first word (with space delimiter) of the first line output by ./inner.sh, the most efficient way is

read firstword _ < <(./inner.sh)

Solution 3 - Bash

You're not splitting on whitespaces but rather on newlines. Give this a whirl:

IFS=$'
'

var=$(./echo.sh)
echo $var | awk '{ print $1 }'
unset IFS

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondjechlinView Question on Stackoverflow
Solution 1 - Bashsampson-chenView Answer on Stackoverflow
Solution 2 - Bashgniourf_gniourfView Answer on Stackoverflow
Solution 3 - BashArnestigView Answer on Stackoverflow