Passing two arguments to a command using pipes

Posted by firebat on Super User See other posts from Super User or by firebat
Published on 2011-08-01T20:54:09Z Indexed on 2014/05/29 9:33 UTC
Read the original article Hit count: 376

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.


UPDATE: 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.

With

file1:
a,b
c,d

file2:
1,2
3,4

Expected output is:

b,1
d,3

RESOLVED:

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.

© Super User or respective owner

Related posts about bash

Related posts about shell-script