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

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.
How to run a command N times in bash
there are many ways to run a command N times in bash/ksh/zsh. Use syntax as per your shell.

ADVERTISEMENTS

Syntax

The syntax is:

## run command 10 times 
for i in {1..10}; do commandNameHere; done
 
## run script 20 times
for i in {1..10}; do /path/to/cache.script.sh; 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.

Using while loop

Use the bash/sh/ksh posix based while loop as follows:

## define end value ##
END=5
## print date five times ##
x=$END 
while [ $x -gt 0 ]; 
do 
  date
  x=$(($x-1))
done

repeat for zsh users

If you are using the Z shell (zsh)

repeat N { command }
repeat N { /path/to/script }
## print date five times on screen ##
repeat 5 { date }

Say hello to seq command

To print a sequence of numbers one can use the seq command too. The syntax is:
seq LAST
seq FIRST LAST
seq FIRST INCREMENT LASE
seq LAST | xargs command
seq FIRST LAST | xargs command
seq FIRST INCREMENT LASE | xargs command

To just print five number, run:
$ seq 1 5
OR
$ seq 5
Sample outputs:

1
2
3
4
5

Use the xargs command to run the date command five times:
$ seq 1 5 | xargs -I{} date
Sample outputs:

Tue Nov 28 00:32:52 IST 2017
Tue Nov 28 00:32:52 IST 2017
Tue Nov 28 00:32:52 IST 2017
Tue Nov 28 00:32:52 IST 2017
Tue Nov 28 00:32:52 IST 2017

You can use xargs with multiple commands line argument as follows:
$ seq 1 5 | xargs -I{} sh -c "date && sleep 1"
It is also possible to use the parallel command:
$ seq 5 | parallel 'echo {}'
$ seq 5 | parallel 'echo Hello $USER, {} times.'

See man page of the following command for more info:
$ man parallel
$ man xargs
$ man seq

A note about using Perl or Python

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")
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
11 comments… add one
  • Trey Blancher May 27, 2011 @ 10:42

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

  • rod May 27, 2011 @ 13:10

    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!

  • JackS May 31, 2011 @ 5:44

    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

  • Serj Jun 12, 2011 @ 14:15

    which is useful tool, repait command with delay

  • loomsen Jun 24, 2011 @ 8:04

    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

  • firejox Aug 22, 2011 @ 9:07

    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}`; }

  • Alex Apr 11, 2012 @ 5:00

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

  • Deepak Apr 15, 2015 @ 8:39

    seq 20 | xargs commandName

  • JayCrav3 Nov 25, 2017 @ 18:48

    [felmo@localhost ~]$ seq 20 | xargs echo “TEST!”
    TEST! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    this is the output from my fedora 27 terminal… how does this work exactly?

    • 🐧 Vivek Gite Nov 27, 2017 @ 18:54

      The seq command display numbers from FIRST to LAST. For example print a sequence of numbers between 1 to 5

      seq 1 5
      seq 5
      seq 1 10
      # between 1 to 10 but increment by 2 #
      seq 1 2 10

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.