Languages
[Edit]
EN

Bash - repeat a character N times

0 points
Created by:
Dirask Admin - Marvin
1820

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

Quick solution:

#!/bin/bash

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

Note: 

The -n attribute prevents every character from being displayed on a newline.

 

Practical example

#!/bin/bash

character="+"

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

Output:

+++

Another solution 

#!/bin/bash

character="+"
text=""

for i in {1..3}
do 
	text="${text}${character}"
done

echo "$text"

Output:

+++

Bash

Bash - repeat a character 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