How to sleep for 1 second between each xargs command?

WDan picture WDan · Mar 1, 2013 · Viewed 11.5k times · Source

For example, if I execute

ps aux | awk '{print $1}' | xargs -I {} echo {}

I want to let the shell sleep for 1 second between each echo.

How can I change my shell command?

Answer

kamituel picture kamituel · Mar 1, 2013

You can use following syntax:

ps aux | awk '{print $1}' | xargs -I % sh -c '{ echo %; sleep 1; }'

Be careful with spaces and semicolons though. After every command between brackets, semicolon is required (even after last one).