vendredi 23 juin 2017

Shell looping vs xargs

For a very long time I was using the following construct to execute a command on foo, bar, and baz (all typed into an interactive shell prompt)

for F in foo bar baz; do command $F ; done

Now I finally got the -n option of xargs (or --max-args if you're using GNU) which limits to number of parameters to pass to the next command. So i can replace the previous command with:

echo foo bar baz | xargs -n 1 command

Here the -n option tells xargs to give at max one parameter to proceed, and create a new command with the next parameter.

As a bonus, instead of constructing an array of numbers with

seq 1 10

don't forget bash and zsh brace expansion


Aucun commentaire: