This question already has an answer here:

I need to split a single line containing multiple words separated by spaces returned by a command. I tried using read but it acts completely strange. For example:

Why does running echo "Hello World!" | read -ra lines; echo "${lines[@]}" in Bash results in an empty line, but running echo "Hello World!" | read -ra lines; do echo "${lines[@]}"; done results in Hello World!?

If I run echo "Hello World!" | tee /dev/stdout | read -ra lines; echo "${lines[@]}" it also results in an empty line.

Even simple echo "Hello World!" | read hello; echo "$hello" doesn't work.

marked as duplicate by αғsнιη, l0b0, eyoung100, Kusalananda bash Jun 13 '18 at 6:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


The second example has a typo. It should be echo "Hello World!" | while read -ra lines; do echo "${lines[@]}"; done

Not the answer you're looking for? Browse other questions tagged or ask your own question.