Passing two arguments to a command using pipes


Usually, we only need to pass one argument:

echo abc | cat
echo abc | cat some_file -
echo abc | cat - some_file

Is there a way to pass two arguments? Something like

{echo abc , echo xyz} | cat
cat `echo abc` `echo xyz`

I could just store both results in a file first

echo abc > file1
echo xyz > file2
cat file1 file2

But then I might accidentally overwrite a file, which is not ok. This is going into a non-interactive script. Basically, I need a way to pass the results of two arbitrary commands to cat without writing to a file.

Sorry, the example masks the problem. While { echo abc ; echo xyz ; } | cat does seem to work, the output is due to the echos, not the cat.

A better example would be { cut -f2 -d, file1; cut -f1 -d, file2; } | paste -d, which does not work as expected.




Expected output is:



Use process substitution: cat <(command1) <(command2)

Alternatively, make named pipes using mkfifo:

mkfifo temp1
mkfifo temp2
command1 > temp1 &
command2 > temp2 &
cat temp1 temp2

Less elegant and more verbose, but works fine, as long as you make sure temp1 and temp2 don't exist before hand.

Best Answer

I think you want 'Process Substitution' It also works on zsh, though it has more options and the syntax may be different.

It creates a pseudo file (/dev/fd/something) for each substitution. It's pretty useful. The command can only read as a stream, meaning it can not go back and forth with fseek. It needs to read it as a stream of bytes, like a pipe.

Your examples work as process substitution:

cat <(echo abc) <(echo xyz)
paste -d, <(cut -f2 -d, file1) <(cut -f1 -d, file2)

Think of it as a tool to use a process and have it pretend to be a file without any temp files.

(from my earlier answer )