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 loop constructs available in that programming language. In Linux bash, we have loops and other methods to repeat commands N number of times efficiently.

In this tutorial, we will explore different bash techniques that allow us to run a certain command multiple times using loops, functions, and command-line utilities.

Creating and Running Bash Scripts

Before exploring the methods, let's understand how to create and execute bash scripts. On Linux or macOS machines, create a bash file using

touch mybash.bash

The file extension can be .bash or .sh. Once you have created the script, execute it using

bash mybash.bash

Using 'for' Loop

The for loop is the most common way to repeat commands. There are different variations available.

Range-based for Loop

#!/bin/bash
# using a for loop with range

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

This produces the following output

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

One-liner for Loop

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

C-style for Loop

#!/bin/bash
# using C-style for loop
for ((i=1; i<=5; i++)); do
   echo "Sr-no: $i - Welcome to Tutorialspoint"
done

Using 'while' Loop

The while loop provides another way to repeat commands based on a condition.

#!/bin/bash
# using a while loop
i=1
while [[ $i -le 5 ]]; do
   echo "Sr-no: $i - We all love Linux."
   ((i++))
done

This produces the following output

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 Functions

You can create reusable functions to repeat commands with different parameters.

#!/bin/bash
# using a bash function

function repeatNTimes(){
   for ((i=1; i<=$1; i++)); do
      eval "${@:2}"
   done
}

repeatNTimes 5 echo "The tougher it gets, the cooler I get"

This produces 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

Using Command-line Utilities

Using 'seq' Command

# Using seq with for loop
for i in $(seq 1 5); do echo "Iteration: $i"; done

Using 'yes' Command

# Print same message 5 times
yes "Hello Linux" | head -5

Using 'watch' Command

# Run command every 2 seconds, 5 times
timeout 10 watch -n 2 'echo "Current time: $(date)"'

Comparison of Methods

Method Best Use Case Flexibility Readability
For Loop (Range) Simple counting Medium High
For Loop (C-style) Complex conditions High Medium
While Loop Dynamic conditions High High
Functions Reusable code High High
Command-line Tools One-time tasks Low Medium

Conclusion

Linux bash offers multiple approaches to repeat commands, including for loops, while loops, custom functions, and command-line utilities. Choose the method that best fits your specific use case for loops for simple repetition, while loops for dynamic conditions, and functions for reusable code blocks.

Updated on: 2026-03-17T09:01:38+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements