Passing two arguments to a command using pipes
I think you want 'Process Substitution' http://tldp.org/LDP/abs/html/process-sub.html. 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 )
The curly braces thingy actually worked because the pipe operator will connect the standard outputs of the grouped commands into the standard input of the third.
You can tell the printout is not from the echos by running any command that does not use standard input, e.g. { echo 1; echo 2;} | echo 3
will print 3 as opposed to 1\n2\n3.
However, this is not the thing you wanted because the two commands in the curly braces are writing to the same output which is like cat
-ing their results. To generate two different results to use as the two arguments for commands like paste
, you'll have to use a temporary file or a named pipe.
You can use mktemp
to generate a unique temporary file or mkfifo
to create a FIFO pipe file.
I like xargs As an example I want to find the size of all the mysql data dir under /usr/local
osx:local user$ pwd /usr/local osx:local user$ ls | grep mysql mysql mysql-5.0.51a-osx10.5-x86_64 mysql-5.0.51b-osx10.5-x86_64 mysql-5.1.39-osx10.5-x86_64 mysql-5.6.17-osx10.7-x86_64 os x:local user$ ls | grep mysql |sudo xargs du -sh 4.0K mysql 2.8G mysql-5.0.51a-osx10.5-x86_64 10G mysql-5.0.51b-osx10.5-x86_64 25G mysql-5.1.39-osx10.5-x86_64 753M mysql-5.6.17-osx10.7-x86_64 osx:local user$
xargs