Say I have a loop that goes through a file line-by-line via read
. I would like to have both the original line, and the line split into different variables, which read
does nicely. However, I can't seem to echo a variable, pipe it to read
, and have the variables populated. Here's a base case:
echo "a b c d e f g" | read line
echo "Read obtained: $line"
The result:
Read obtained:
How can I make read do what I want? Or: Why am I wrong to ask read to do this, and what should I do instead?
read
is a built-in, so it hasn't actually been executed in your top level script/shell. And also, don't use a shell loop to process text! – Wildcard Sep 09 '17 at 03:48read
were to be able to assign to a variable in the outer scope, your snippet of code is pretty much (but TBH not exactly) the same as a simple assignment:line="a b c d e f g"
. – Chris Davies Sep 09 '17 at 09:13