Baltar27
Baltar27

Reputation: 31

Sending two processes ouput pipes to a dual pipe input process

I have two commands (command 1 and command2) that output to stdout (fd 1) and I'd like to send them to a new command3 that is prepared to receive them in two pipes, one on stdin from command1 and the other in any other file descriptor, i.e. in the fd 3, from command2. How can I do this in bash ?

Upvotes: 3

Views: 226

Answers (1)

rkachach
rkachach

Reputation: 17345

This could be done by using process subsitution technique, from bash ref:

Process substitution allows a process’s input or output to be referred to using a filename. It takes the form of

<(list)

or

>(list)

The process list is run asynchronously, and its input or output appears as a filename.

Using this technique basically you can read the output of a command (list in the above example) as if you were reading from a file. In fact, you can have several inputs which can solve your problem as following:

command3 <( command1 ) <( command2 )

For this, you have to open both files (received as arguments) and read from them.

The process substitution basically creates a file (/dev/fd/XX) and uses its name as input to the receiving command (command3 in the above example). Please, Keep in mind that both the commands command1 and command2 will run asynchronously, thus you can't expect/rely on any execution order when launching the above command.

Upvotes: 1

Related Questions