Run two commands at same time and pipe to third command

I currently want to run in my terminal cmd1 and cmd2. I want to pipe these outputs to cmd3, which can take multiple inputs.

I tried

cmd1 && cmd2 | cmd3 -i - -i -

But this does not accomplish what I'm trying to do

1 Answer

To answer your question literally ("Run two commands at same time and pipe to third command ")

You would need to put at least one of the LHS commands into the shell background using &, and group them so that the combined standard output may be redirected ex.

{ cmd1 & cmd2; } | cmd3 -i - -i -

Note that & is different from && (which waits for cmd1 to exit, then conditionally executes cmd2).

You could replace the command group { ... ; } with a subshell ( ... ) if you prefer. Since the shell has only one standard output and one standard input stream, this will merge the outputs of cmd1 and cmd2


To answer what you actually seem to want

To redirect the outputs of two separate commands as two separate input streams in bash, you can use process substitution:

cmd3 -i <(cmd1) -i <(cmd2)
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like