While loop substitution is vs. here is the line with the replacement command

Can someone explain the difference between these two loops:

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

-

 while read test; do echo $test done < <(seq 5) 
+4
source share
2 answers
 while read test; do echo $test done <<< "$(seq 5)" 

Run seq 5 , collecting the result in a temporary variable. Then run a while loop, yielding the result of the collection.

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

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

What's the difference? For seq 5 almost nothing; however, it can be made visible by changing seq 5 to seq 5; echo done generating sequence >&2 seq 5; echo done generating sequence >&2 . Then you can see that in the first case, the execution of all seq ends before the start of the while , and in the second case, they are executed 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 a seq 10000000 , the difference would be much clearer. The form <<<"$(...) would use a lot more memory to store the time line.

+6
source

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

If you do echo <(:) , you see the difference in the concept of replacing a process with other string inputs.

Process substitution is more like a file representation, while here strings are more sent to the input buffer.

+2
source

Source: https://habr.com/ru/post/1498766/


All Articles