How to repeat a character 'n' times in Bash

https:/‮tual.www/‬turi.com
How to repeat a character 'n' times in Bash

To repeat a character n times in Bash, you can use the printf command with the %*s format specifier.

For example, to print the character x 10 times, you can use the following command:

printf "%*s" 10 x

This will print the string "x" with a width of 10 characters, effectively repeating the character x 10 times.

You can also use the yes command to repeat a character or string multiple times. For example, to print the character x 10 times, you can use the following command:

yes x | head -n 10

This will repeat the string "x" until the head command terminates after receiving 10 lines of input.

You can also use a loop to repeat a character multiple times. For example, the following script will print the character x 10 times:

#!/bin/bash

for i in {1..10}
do
  printf "x"
done

This script uses a for loop to iterate over a range of numbers from 1 to 10, and the printf command to print the character x on each iteration.

Created Time:2017-10-28 21:39:07  Author:lautturi