while read line
do
my_array=("${my_array[@]}" $line)
done
echo ${my_array[@]}
If you just run it, it will keep reading from standard-input until you hit Ctrl+D (EOF). Afterwards, the lines you entered will be in my_array. Some may find this code confusing. The body of the loop basically says my_array = my_array + element.
is the way of assigning to an array. Using it in conjunction with command substitution, you can read in arrays from pipeline which is not possible to use read to accomplish this in a straight-forward manner:
echo -e "a\nb" | read -a arr
echo ${arr[@]}
You will find that it output nothing due to the fact that read does nothing when stdin is a pipe since a pipeline may be run in a subshell so that the variable may not be usable at all.
Here's one way to do it:
If you just run it, it will keep reading from standard-input until you hit Ctrl+D (EOF). Afterwards, the lines you entered will be in
my_array
. Some may find this code confusing. The body of the loop basically saysmy_array = my_array + element
.Some interesting pieces of documentation:
The Advanced Bash-Scripting Guide has a great chapter on arrays
The manpage of the read builtin
15 array examples from thegeekstuff.com
Read it using this:
And for printing, use:
And one that doesn't recreate the array each time (though requires bash 3.1 or newer):
See http://mywiki.wooledge.org/BashFAQ/001 for more.
And as always, to avoid writing bugs read http://mywiki.wooledge.org/BashGuide and avoid the tldp-guides like the Advanced bash scripting guide.
How about this one-liner ;)
Edit:
In
bash
,is the way of assigning to an array. Using it in conjunction with command substitution, you can read in arrays from pipeline which is not possible to use
read
to accomplish this in a straight-forward manner:You will find that it output nothing due to the fact that
read
does nothing whenstdin
is a pipe since a pipeline may be run in a subshell so that the variable may not be usable at all.Using the way suggested by this answer:
It gives
a b
which is way simpler and more straight-forward than any workaround given by the answers of Read values into a shell variable from a pipe and in bash read after a pipe is not setting values.OUTPUT