Programmatic process substitution in bash

by Anton K   Last Updated September 11, 2019 14:01 PM

As one can found in the docs, the head <(yes) <(yes n) will first substitute <(yes) and <(yes n) with a temporary filename, pointing to pipe which will produce the yes or yes no respectively output while being read, so the output will be ten y and ten n chars.

However, the following will not work:

head $(echo "<(yes) <(yes n)")                                                      
head: cannot open '<(yes)' for reading: No such file or directory
head: cannot open '<(yes' for reading: No such file or directory
head: cannot open 'n)' for reading: No such file or directory

How to overcome this issue and get command to work with echo and any other generated process substitution?



Answers 1


Your command with the quotes won't work, because the process substitution symbols retain their literal value when quoted.

You would think that

head $(echo <(yes) <(yes n))

would work, but this also fails with the message:

head: cannot open '/dev/fd/63' for reading: No such file or directory
head: cannot open '/dev/fd/62' for reading: No such file or directory

I believe that the reason for this is that the head command is invoked after the echo command has already completed, so the temporary file descriptors have already been deleted by the time the head command is run.

Add some more details to your question exactly what you are trying to do, maybe there is a solution that isn't apparent from this minimal example.

user000001
user000001
September 11, 2019 14:01 PM

Related Questions



How can I use two parameters at once with xargs?

Updated April 23, 2017 12:01 PM

Bash: receive messages from background process

Updated August 28, 2018 12:01 PM