Languages
[Edit]
EN

Bash - repeat a command N times

0 points
Created by:
Dirask Admin - Marvin
1820

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

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