How to repeat a character ‘n’ times in Bash

Let us say you want to repeat a character such as ‘-‘ OR ‘=‘ multiple times while writing bash script. Please note that I needed a quick script to work on Linux, macOS, and FreeBSD server to repeat a string/character n times. One simple way to to repeat a string or character n times is simple use the echo command/printf command as follows:
echo '------------------------------------'
printf '%s\n' '------------------------------------'


But, there must be a better way. For instance, here are classic Perl and Python examples that repeat ‘-‘ 80 times:

perl -E 'say "-" x 80'

Python one-liner:

python -c 'print "-" * 80'

How to repeat a character ‘n’ times in Bash

Let us try this example Linux and Unix Bash function to repeat a string/character ‘n’ times with help of bash for loop as follows:

# Repeat given char 80 times using shell function
repeat(){
	for i in {1..90}; do echo -n "$1"; done
}

Then all I have to do is call repeat() as follows:

repeat '-'
echo
 
repeat '='
echo
 
repeat '~'; echo -e '\nHEAD 1\t\tHead 2\t\tHead 3\t\t\t\tHead 4'; repeat '~'; echo


My solution is simple and friendly. It solved my problem. Yet now I am getting greedy. I wanted to see if I could use printf/echo and replicate the Perl/Python one-liner experience.

Repeating a character or string n times using printf

The printf is a shell built-in and external /bin/printf, used to format and prints ARGUMENTS. The syntax is:

printf 'FORMAT' var
printf '%s\n' 'Hello World'
printf -- '-%.0s' 1 2 3 4 5
printf -- '-%.0s' {1..5}
printf -- '-%.0s' {1..80}

Where,

  • The double dash “--” means “end of command line flags.” Since I need to print -, the double dash (--) tells printf command not to try to parse what comes after command line options.
  • Bash expands {1..5}. For example, echo {1..5}, prints 1 2 3 4 5. So
    printf -- '-%.0s\n' {1..5}
    becomes:
    printf -- '-%.0s\n' 1 2 3 4 5
  • The -%0s print single ‘-‘ given 5 times.
  • You can remove double dash (--) when not printing '-‘. For instance:
    printf '~%.0s\n' {1..80}

Closing note

I ended up using the following as I needed to run script on both macOS/BSD and Linux:

# Repeat given char N times using shell function
repeat(){
	local start=1
	local end=${1:-80}
	local str="${2:-=}"
	local range=$(seq $start $end)
	for i in $range ; do echo -n "${str}"; done
}

Then I run it as follows:

repeat 80 '-'; echo
 
repeat 40 '='; echo
 
# print Hello 5 times 
repeat 5 'Hello'; echo

I am sure someone could come up with awk, sed, echo/printf and other versions too. But, for now, I am satisfied with what I got. As usual, check man pages:
man bash
help printf
man seq


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Harshvardhan Gupta Jun 4, 2021 @ 11:25

    You can use powershell core on Linux and write: “*” * 25

  • Bit Twiddler Jun 5, 2021 @ 8:47

    Nice! I’d previously used things like:

    head -c 80 ​< /dev/zero | tr '\0' '.'

    • Anonymous Jun 6, 2021 @ 19:48

      Wow. Never thought of that.

  • Cüneyt Egin Jun 5, 2021 @ 18:44

    How about:
    seq -s_ 200|tr -d '[:digit:]'
    to print a line with 200 underscores…

  • Dumitru Uzun Jun 6, 2021 @ 5:40

    There is a small typo in the last line of the last ‘repeat()’:
    It should be “$str” instead of “$2”

  • Rehman Jun 6, 2021 @ 12:25

    As usual, awesome post for Bash lovers like me.

    • Didi Jun 6, 2021 @ 19:48

      +1 from me too.

  • Bruce M Jun 9, 2021 @ 11:00
    #!/bin/sh
    # repeat times char
    if test $# -ne 2 ; then
        echo 1>&2 usage: $(basename $0) times char
        exit 1
    fi
    yes "$2" | head -n "$1" | tr -d '\n'
    

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum