Using the results of a command of as a variable is pretty easy. Just use the venerable $(some command)
, as in this simple example:
for i in $(ls | grep -v badpattern); do
mv $i $i.bak
done
But what if you want to use the results of multiple commands as file inputs to another command? I found this extremely handy syntax recently:
diff <(svn cat svn+ssh://somerepo/somefile@5342) \
<(svn cat svn+ssh://somerepo/somefile@13957)
This command substitutes the commands inside the <(command)
with file descriptors. As far as diff
knows, it’s comparing two files, not two commands.*
The syntax goes the other way, as well:
tar cf >(bzip2 -c > file.tar.bz2) somedirectory/
Here, the results of tar cf somedirectory/
are piped through bzip2 -c > file.tar.gz2
, compressing the file without a second command.**
More information at The Linux Documentation Project: http://tldp.org/LDP/abs/html/process-sub.html
* I realize you could normally just do svn diff -r 5342:13957 somefile
here, but these were deleted files, which are a pain in subversion. This was easier.
** The -j
tar flag does the same thing with less typing. Again, for the sake of the example.