Linux command to repeat a string n times

Solution 1:

adrian@Fourier:~$ printf 'HelloWorld\n%.0s' {1..5}
HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
adrian@Fourier:~$

Solution 2:

Here's an old-fashioned way that's pretty portable:

yes "HelloWorld" | head -n 10

This is a more conventional version of Adrian Petrescu's answer using brace expansion:

for i in {1..5}
do
    echo "HelloWorld"
done

That's equivalent to:

for i in 1 2 3 4 5

This is a little more concise and dynamic version of pike's answer:

printf -v spaces '%*s' 10 ''; printf '%s\n' ${spaces// /ten}

Solution 3:

Quite a few good ways already mentioned. Can't forget about good old seq though:

[john@awesome]$for i in `seq 5`; do echo "Hi";done
Hi
Hi
Hi
Hi
Hi

Solution 4:

This can be parameterized and doesn't require a temp variable, FWIW:

printf "%${N}s" | sed 's/ /blah/g'

Or, if $N is the size of a bash array:

echo ${ARR[@]/*/blah}