I need to store specific number of spaces in a variable.
I have tried this:
i=0
result=""
space() {
n=$1
i=0
while [[ "$i" != $n ]]
do
result="$result "
((i+=1))
done
}
f="first"
s="last"
space 5
echo $f$result$s
The result is "firstlast", but I expected 5 space characters between "first" and "last".
How can I do this correctly?
echo "first$(printf '%*s' 5 ' ')last"
should do the trick without looping.printf 'first%*slast\n' 5 ' '
too. Use something likespaces=$(printf '%*s' 5 ' ') ; echo "|$spaces|"
to put the spaces into a variable and use them... – Sep 02 '14 at 13:40