I’m trying to use the bash builtin read to read data from a file or from standard input (doesn’t matter which for the purpose of the discussion, as long as I can pipe it in), and store them in an array. This sometimes works and sometimes not, and I’m not sure why.
Here is the test case:
values=(1 2 3)
echo "4 5 6" | read -a values; echo "${values[*]}"
The output should be “4 5 6”, but instead its “1 2 3”. On the other hand, when running it like this:
values=(1 2 3)
echo "4 5 6" | ( read -a values; echo "${values[*]}" )
I get the correct output.
Any ideas?
BASH FAQ entry #24: “I set variables in a loop. Why do they suddenly disappear after the loop terminates? Or, why can’t I pipe data to read?”