How to Run a Command Multiple Times in Linux

Take Your Linux Skills to the Next Level All courses, certifications, ad-free articles & community — from $8/mo
Join Root →
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
From $8/mo · or $59/yr billed annually · Cancel anytime

For one reason or the other, you may wish to run a command repeatedly for several times in Linux. This guide will discuss some of the common and effective ways to achieve just that. Let’s consider the first method.

Note that if you intend to run a command after command after every x seconds, you can check out – How to Run or Repeat a Linux Command Every X Seconds

Run a Command Multiple Times in Linux using a Bash for Loop

The easiest way to repeat a command on the Bash shell is to run it in for a loop. You can use the following syntax, where a counter is a variable (you can give it a name of your choices such as i or x etc.) and n is a positive number to represent the number of times you want the command to run:

for counter in {1..n}; do yourCommand_here; done

Here is an example:

$ for x in {1..10}; do echo "Tecmint.com - The #1 Linux blog $x"; done
Run Command Multiple Times in Linux
Run Command Multiple Times in Linux

Run a Command Multiple Times in Linux using a while Loop

Related to the previous method, a while loop can also be used to run a command many times in Linux using the following syntax:

$ i=1; while [ $i -le n ]; do yourCommand_here; i=$(($i++)); done
OR
$ i=1; while [ $i -le n ]; do yourCommand_here; ((i++)); done

In the above format, i represents the counter variable, [ $i -le n ] is the test condition and n is the number of times you wish to run the command (ideally the number of times the shell will iterate through the loop.

Another important part of the while loop is i=$(($i+1)) or (($i++)) which increments the counter until the test condition becomes false.

So you can run your command many times like this (replace 10 with the number of times you wish to repeat the command):

$ i=1; while [ $i -le 10 ]; do echo "Tecmint.com - The #1 Linux blog $i";((i++)); done
Run Command Multiple Times Using While Loop
Run Command Multiple Times Using While Loop

Run a Command Multiple Times Using seq Command

The third means of running a command several times in Linux is by using the seq command which prints a sequence of numbers incrementally in conjunction with the xargs command in this form:

$ seq 5 | xargs -I -- echo "Tecmint.com - The #1 Linux blog"

To add the count at the end of each command, use this syntax:

$ seq 5 | xargs -n 1 echo "Tecmint.com - The #1 Linux blog"
Run Command Multiple Times Using Seq
Run Command Multiple Times Using Seq

Also, check these related articles:

That’s all for now. If you know of other methods for running a command multiple times in Linux, let us know in the comments section below.

Root Plan
Premium Linux Education for Serious Learners

Take Your Linux Skills to the Next Level

Root members get full access to every course, certification prep track, and a growing library of hands-on Linux content — with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Root Plan Premium Linux Education for Serious Learners

Before You Go - Upgrade Your Linux Skills

Root members get everything in one place, with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Linux certifications: RHCSA, RHCE, LFCS and LFCA
Access new courses on release
Weekly newsletter, priority support & Telegram community
Join Root Today and Start Learning Linux the Right Way
Structured courses, certification prep, and a community of Linux professionals - all in one membership.
Join Root Plan →
$8/mo · or $59/yr billed annually