Languages
[Edit]
EN

Bash - repeat a command N times

0 points
Created by:
martineau
1170

This article will show you how to repeat command N times in Bash.

Quick solution:

#!/bin/bash

for i in {1..N}
do 
	<your commands>
done

or:

#!/bin/bash

for ((i = 0; i < N; i++))
do
    <your commands>
done

 

Practical examples

Example 1

#!/bin/bash

character="+"

for i in {1..5}
do 
	echo -n "$character"
done

Output:

+++++

Example 2

#!/bin/bash

for i in {1..3}
do
    date
done

Output:

Mon Jun 28 18:01:18 UTC 2021
Mon Jun 28 18:01:18 UTC 2021
Mon Jun 28 18:01:18 UTC 2021

Alternative titles

  1. Bash - repeat a variable N times
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Bash

Bash - repeat a command N times
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join