3

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?

  • Pipelines create a subshell and `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!](http://unix.stackexchange.com/q/169716/135943)** – Wildcard Sep 09 '17 at 03:48
  • Even if `read` 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"`. – roaima Sep 09 '17 at 09:13
  • I understand. I was only presenting a minimal case to demonstrate. – Matthew Curry Sep 09 '17 at 23:52

2 Answers2

1

You can tell read read from pipeline as follows; Original answer by @yardena on SO

echo "a b c d e f g" | { read line; echo line=$line; }
αғsнιη
  • 40,939
  • 15
  • 71
  • 114
0

the problem is scope. the variable has no value outside the subshell created by the pipe. Instead:

while read line; do echo "read obtained: ${line}"; done < <(echo "a b c d e f g")

use a loop however you want to. if you want to process text with one, do it. -C

αғsнιη
  • 40,939
  • 15
  • 71
  • 114
chris
  • 21
  • 2
  • You should actually read the linked post thoroughly. [Why is using a shell loop to process text considered bad practice?](http://unix.stackexchange.com/q/169716/135943) – Wildcard Sep 09 '17 at 04:09