Multiplying strings in bash script
You can use bash
command substitution
to be more portable across systems than to use a variant specific command.
$ myString=$(printf "%10s");echo ${myString// /m} # echoes 'm' 10 times
mmmmmmmmmm
$ myString=$(printf "%10s");echo ${myString// /rep} # echoes 'rep' 10 times
reprepreprepreprepreprepreprep
Wrapping it up in a more usable shell-function
repeatChar() {
local input="$1"
local count="$2"
printf -v myString '%*s' "$count"
printf '%s\n' "${myString// /$input}"
}
$ repeatChar str 10
strstrstrstrstrstrstrstrstrstr
You could simply use loop
$ for i in {1..4}; do echo -n 'm'; done
mmmm
That will do:
printf 'f'; printf 'o%.0s' {1..2}; echo
Look here for explanations on the "multiplying" part.