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++)) do command1 command2 done
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:
#!/usr/bin/perl for $i (1 .. 5) { system("date"); }
Python example:
#!/usr/bin/python # Run unix date command 3 times import os; for x in range(0,3): os.system("date")
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











![Unix Copy Command Examples [ cp command ]](http://s13.cyberciti.org/images/shared/rp/3/10.jpg)

{ 7 comments… read them below or add one }
I always forget the list iterator {1..10} syntax for bash. Thanks for the reminder!
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!
Thanks for the heads up!
Moin,
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
Hi.
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
Hi,
I have another way to implement this.
just use seq and xargs
function __my_test { test $# -gt 1 && `seq 0 $1|xargs -i ${*#$1}`; }
Thanks for this :) Whenever I google something for linux/bash help I’m normally taken here XD