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.





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/; 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.

Using while loop

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

## define end value ##
## print date five times ##
while [ $x -gt 0 ]; 

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 LAST | xargs command
seq FIRST LAST | xargs command
seq FIRST INCREMENT LASE | xargs command

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


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:

for $i (1 .. 5) {

Python example:

# Run unix date command 3 times 
import os;
for x in range(0,3):
0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
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
Creating SWAP partition using FDISK & FALLOCATE commands
Viewed 1149 times since Thu, Jan 16, 2020
LVM: Reduce SWAP size by removing a Logical Volume
Viewed 1117 times since Sat, Jun 2, 2018
Fix rpmdb: Thread died in Berkeley DB library
Viewed 19419 times since Fri, Feb 14, 2020
Tunnel SSH Connections Over SSL Using ‘Stunnel’ On Debian 7 / Ubuntu 13.10
Viewed 1564 times since Fri, Sep 28, 2018
Linux: Disks diagnostic using smartctl
Viewed 12262 times since Wed, Jul 25, 2018
Migrate a Linux System from Red Hat Enterprise to CentOS
Viewed 8990 times since Fri, May 15, 2020
8 Vim Tips And Tricks That Will Make You A Pro User
Viewed 1794 times since Fri, Apr 19, 2019
20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management YUM
Viewed 9948 times since Thu, Oct 25, 2018
RHEL: Reserved space on a ext2/ext3/ext4 filesystem
Viewed 3079 times since Sun, May 27, 2018
Fałszujemy rozpoznania skanerów #1
Viewed 1964 times since Mon, May 21, 2018