Adding extra commands inbetween xargs commands

56 Views Asked by At

I have a bash script like the following example that attempts to first list a bunch of items, then run a command on those items individually:

customListCommand | xargs -P 10 -n1 echo

If customListCommand returns the following output:

a
b
c

Then what I have with xargs will essentially perform:

echo a
echo b
echo c

In addition to this, I would like to add a "sleep" to the xargs command so that it runs after each echo, in this manner:

echo a
sleep 1
echo b
sleep 1
echo c
sleep 1

Is it possible to do this for the xargs expression? I presume adding an additional pipe at the end will not work since that might pipe the output of all the echos before doing a single sleep.

2

There are 2 best solutions below

0
Hazem Hagrass On BEST ANSWER

Did you try something like that??

customListCommand | xargs -P 10 -n1 bash -c 'echo "$0"; sleep 1'

It should basically do the job for you. Please let me.know if more help is required!

1
Cyrus On

I suggest to use this:

customListCommand | xargs -I {} bash -c 'echo {}; sleep 1'