0

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.

0

1 Answer 1

0

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 .