xargs

[last updated - 05 August 2003]

Sometimes you will want to do something once for each item in a list. I can't predict when the occassion will arise but I can tell you the tool to use to do this in a simple way. It is called xargs. Type in the following at the command prompt:

echo aa bb cc dd

You see aa bb cc dd as you would expect. Now pipe it to awk and get awk to print what it sees like this:

echo aa bb cc dd | awk '{print}'

You see aa bb cc dd again. But suppose you wanted to call awk once for each of aa bb cc dd? You can use xargs for this. Think of it as meaning "transfer arguments". You can tell xargs the number of arguments you want transferring. Try it with two like this:

echo aa bb cc dd | xargs -n2 | awk '{print}'

and finally with one like this:

echo aa bb cc dd | xargs -n1 | awk '{print}'

There is more to xargs than just this. At this stage you only need to be aware that it is there and a tool to possibly use in the future.

Go back to the home page.

E-mail the macro and web site author.