How to Run a Command Multiple Times in Linux?


There are scenarios where you would want to run a particular command for 'N' number of times. In normal programming, this can be done with the help of any of the loops constructs available in that programming language. In Linux bash too, we have loops available as some other ways with which we can repeat commands 'N' number of times.

In this tutorial, we will explore different bash scripts that will allow us to run a certain command multiple times.

The first step when you want to work with a bash script is to create one. On Linux or macOS machines, you can create a bash file using the following command −

touch mybash.bash

Notice that the name of the file can be anything, but the extension of the file must be the same (i.e., bash). Once you have saved the bash script, use the following command to execute it −

bash mybash.bash

Now that we know how to create a bash file and run it, let's explore all these different examples.

Repeating a Command Using 'for' Loop

You can use a 'for' loop to print a certain command multiple times. There are different variations of the 'for' loop, and we will explore all of them with the help of different bash scripts.

Let's first consider a simple 'for' loop that is not condition based, where we would print a simple command 5 times. Consider the following bash script −

# using a for loop

for i in {1..5}
do
   echo 'Sr-no: $i - Welcome to Tutorialspoint'
done

Save this script and then execute it. You will get the following output on the terminal −

Sr-no: 1 - Welcome to Tutorialspoint
Sr-no: 2 - Welcome to Tutorialspoint
Sr-no: 3 - Welcome to Tutorialspoint
Sr-no: 4 - Welcome to Tutorialspoint
Sr-no: 5 - Welcome to Tutorialspoint

Another simpler way to achieve the same is to write the above bash script code in a single line, which might look a little weird at first, but does the same job in less number of lines.

# using a for loop
for i in {1..5}; do echo 'Sr-no : $i - Welcome to TutorialsPoint'; done

Both the above bash scripts use non-conditional 'for' loops, but you can use condition-based 'for' loops too to print a certain command 'N' number of times. For example, take a look at the following bash script −

# using a for loop
for ((i=0;i<5;i++)); do echo 'Sr-no : $i - Welcome to TutorialsPoint'; done

Execute the above bash script and you will notice that it produces the same output.

Using 'while' Loop

You can use a 'while' loop too in a Bash script to print a certain command multiple number of times. For example, consider the bash script shown below.

# using a while loop
i=1
while [[ $i -le 5 ]]; do
   echo 'Sr-no: $i - We all love Linux.'
   let ++i;
done

Run the above script and it will produce the following output on the terminal: −

Sr-no: 1 - We all love Linux.
Sr-no: 2 - We all love Linux.
Sr-no: 3 - We all love Linux.
Sr-no: 4 - We all love Linux.
Sr-no: 5 - We all love Linux.

Using Bash Function

You can use a Bash function to print a certain command multiple number of times. For example, consider the bash script shown below.

# using a bash function

function repeatNTimes(){
   for ((i=0;i<$1;i++));
   do
      eval ${*:2}
   done
}
repeatNTimes 5 echo 'The tougher it gets, the cooler I get'

Here we have created a user-defined function called 'repeatNTimes' and then called it to print a string 5 times. Save and execute this bash script. You will get the following output

The tougher it gets, the cooler I get
The tougher it gets, the cooler I get
The tougher it gets, the cooler I get
The tougher it gets, the cooler I get
The tougher it gets, the cooler I get

Conclusion

In this tutorial, you learned different ways of repeating a command in Linux multiple times.

Updated on: 19-Jul-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements