HTF
HTF

Reputation: 7270

While loop - process substitution vs. here string with command substitution

Can someone please explain the difference between these two while loops:

while read test; do
echo $test
done <<< "$(seq 5)"

-

while read test; do
echo $test
done < <(seq 5)

Upvotes: 13

Views: 4207

Answers (2)

rici
rici

Reputation: 241761

while read test; do
  echo $test
done <<< "$(seq 5)"

Execute seq 5, collecting the result into a temporary variable. Then execute the while loop, feeding it the collecting result.

while read test; do
  echo $test
done < <(seq 5)

Set up a subshell to execute seq 5 and connect its stdout to stdin. Then start the while loop. When it finishes, restore stdin.

What's the difference? For seq 5, practically nothing; however, it can still be made visible by changing seq 5 to seq 5; echo done generating sequence >&2. Then you can see that in the first case, the entire seq execution finishes before the while loop starts, while in the second case they execute in parallel.

$ while read n; do echo $n > /dev/stderr; done \
>       <<<"$(seq 5; echo done generating sequence >&2)"
done generating sequence
1
2
3
4
5
$ while read n; do echo $n > /dev/stderr; done \
>       < <(seq 5; echo done generating sequence >&2)
1
2
done generating sequence
3
4
5

If it were seq 10000000, the difference would be much clearer. The <<<"$(...) form would use a lot more memory in order to store the temporary string.

Upvotes: 18

konsolebox
konsolebox

Reputation: 75488

Based on what I perceive, the only difference is that process substitution would represent a named pipe e.g. /dev/fd/63 as a file for input whereas <<< "" would send the input internally as if reading a buffer. Of course is the command reading the input is on another process like a subshell or another binary, then it would be sent to it like a pipe. Sometimes in environments where process substitution is not possible like in Cygwin, here documents or here strings along with command substitutions are more helpful.

If you do echo <(:) you see the difference in concept for process substitution over other string inputs.

Process substitution is more of representing a file, while here strings are more of sending in a buffer for input.

Upvotes: 4

Related Questions