Is there any way to read lines from command output?

I have a pre process command to output a file

./ > preprocessed_file 

and the preprocessed_file will be used like this

while read line

    ./ $line &

done < preprocessed_file 

rm -f preprocessed_file

Is there any way to direct the output to the while read line part instead of outputting to the preprocessed_file? I think there should be a better way other than using this temp preprocessed_file.

Asked By: Marcus Thornton


You can use bash process substitution:

while IFS= read -r line; do
  ./ "$line" &
done < <(./

Some advantages of process substitution:

  • No need to save temporary files.
  • Better performance. Reading from another process often faster than writing to disk, then read back in.
  • Save time to computation since when it is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion
Answered By: cuonglm

Yes! You can use a process pipe |.

./ |
    while IFS= read -r line
        ./ "$line" &

A process pipe passes the standard output (stdout) of one process to the standard input (stdin) of the next.

You can optionally put a newline character following a | and extend the command to the next line.

Note: a|b is equivalent to b < <(a), but without the magic files, and in a more readable order, especially when the pipeline gets longer.

a|b|c is equivalent to c < <(b < <(a))


a|b|c|d|e is e < < (d < <(c < <(b < <(a))))

Answered By: ctrl-alt-delor
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.