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

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

last updated in Categories , ,

 

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.

 

 

 

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")
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
RHCS6: Show/Add GFS2/GFS journals
Viewed 12598 times since Sun, Jun 3, 2018
Applescript: Run or Call a Shell Script
Viewed 4096 times since Tue, Aug 6, 2019
Zabijanie wszystkich procesów użytkownika
Viewed 2556 times since Thu, May 24, 2018
How to enable Proxy Settings for Yum Command on RHEL / CentOS Servers
Viewed 12284 times since Thu, Jul 19, 2018
How to Install and use Lsyncd on CentOS 7 / RHEL 7 rsync
Viewed 4148 times since Wed, Oct 31, 2018
How do I add ethtool settings to a network device permanently?
Viewed 6441 times since Mon, May 21, 2018
What UUIDs can do for you
Viewed 1692 times since Tue, Jul 17, 2018
How to enable automatic security updates on CentOS 7 with yum-cron
Viewed 2375 times since Fri, Oct 26, 2018
LVM: Create a new Volume Group
Viewed 1968 times since Sat, Jun 2, 2018
How to do a Filesystem Resize (ext3/ext4) on Redhat running on VMware
Viewed 10893 times since Wed, Jul 25, 2018