LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: functional difference between process substitution and here string with $( ) ? (https://www.linuxquestions.org/questions/programming-9/bash-functional-difference-between-process-substitution-and-here-string-with-%24-795426/)

catkin 03-14-2010 10:55 PM

Bash: functional difference between process substitution and here string with $( ) ?
 
Hello :)

Further to solved LQ thread Bash: how to populate a list of arbitrarily named files?, what is the functional difference between feeding a loop with process substitution and feeding it with a here string with embedded command substitution?

ABSG pages: process substitution, here string and command substitution.

This works
Code:

while IFS= read -r -d '' file
do
  files+=("$file")
done < <(find $dir -type f -print0)

but this does not work
Code:

while IFS= read -r -d '' file
do
  files+=("$file")
done <<< "$(find $dir -type f -print0)"

Best

Charles

tuxdev 03-15-2010 01:47 AM

Process substitution sets up a FIFO and expands to the device file that fifo is associated with (/dev/fd/42 or somesuch). As such, all data from the command is redirected, doesn't matter what's in it.

The command substitution and here-string breaks because bash first does the command substitution, which chomps newlines from the end and cuts everything past a null in the data, since nulls aren't allowed in a string. Then, the here-string actually creates a here-document which actually creates a temporary file containing the string plus one newline at the end (since we chomped all of them earlier, but a valid text file must end each line with a newline).

Short story, the only way command substitution and here-string is equivalent to process substitution is when the content has exactly one newline at the end and no nulls whatsoever.

catkin 03-15-2010 02:08 AM

Thank you for that very clear explanation tuxdev :)


All times are GMT -5. The time now is 10:26 AM.