Linux / UNIX: Run Command a Number of Times In a Row

by on May 19, 2011 · 9 comments· LAST UPDATED May 27, 2011


How do I run "foo" command 10 times (or n times) under Linux or UNIX like operating systems?

You can use bash shell loop (run code or command repeatedly) to run a command 10 times as follows:

for i in {1..10}; do commandNameHere; done

For example, run UNIX date command five times, enter:

for i in {1..5}; do date; done

You can also use c like bash for loop syntax:

for ((n=0;n<5;n++))

The for loop syntax for tcsh / csh / ksh and other shell may changes from version to version. You can use python or Perl as follows to run date command 5 times:

for $i (1 .. 5) {

Python example:

# Run unix date command 3 times 
import os;
for x in range(0,3):
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 9 comments… read them below or add one }

1 Trey Blancher May 27, 2011 at 10:42 am

I always forget the list iterator {1..10} syntax for bash. Thanks for the reminder!


2 rod May 27, 2011 at 1:10 pm

Nice and useful article, indeed.
A litte note:

You said “For example, run UNIX date command five times, enter:”

and then shows: for i in {1..10}; do date; done

I mean: five times would be

for i in {1..5}; do date; done

But I know people understood what you’ve said.
Thanks again!


3 nixCraft May 27, 2011 at 4:47 pm

Thanks for the heads up!


4 JackS May 31, 2011 at 5:44 am


old style methods:

for i in $(seq 5);do date;done
for i in `seq 1 5`;do date;done
for i in 1 2 3 4 5;do date;done
for i in a b c d orwhatever;do date;done

at last some crazy unuseful (only work with dirs with more than 5 items)

for i in `ls /proc/*|head -n5`;do date;done


5 Serj June 12, 2011 at 2:15 pm

which is useful tool, repait command with delay


6 loomsen June 24, 2011 at 8:04 am


I got this little function in my bash_aliases:
repeat() { local i n; n=$1; shift; for ((i=1; i<=n; i++)); do "$@"; done; }

then you can do # repeat 5 echo hey there


7 firejox August 22, 2011 at 9:07 am


I have another way to implement this.
just use seq and xargs

function __my_test { test $# -gt 1 && `seq 0 $1|xargs -i ${*#$1}`; }


8 Alex April 11, 2012 at 5:00 am

Thanks for this :) Whenever I google something for linux/bash help I’m normally taken here XD


9 Deepak April 15, 2015 at 8:39 am

seq 20 | xargs commandName


Leave a Comment

Tagged as: , , , , , , , , , ,

Previous Faq:

Next Faq: