Tuesday, March 29, 2011

bash-fu:xargs

The basic techniques of bash-fu are famous now.find, grep, sed, awk, some for loops and everything is at your fingertips. The second level of bash dao is using xargs: the gem of unix tools.

xargs is to bash what map is to functional programming, and it does to input exactly what map does: apply a function to each element in input. However, the problem with xargs is that it accepts only one argument at a time, so what if we want to emulate a double loop? sh comes to help:

xargs -L 1 -I {} sh -c "`foobar| xargs -L 1 -Ix x "

or something like that :D