How to echo formatted string into a variable

bash

Solution

Have you quoted your variable when echoing? If you do, you will see the format is kept.

$ for i in 1 2 3; do     a="${a} `printf "%-10s %s" "hello" "world"`"; done
$ echo "$a"
 hello      world hello      world hello      world

While not quoting destroys everything in the format:

$ echo $a
hello world hello world hello world

Problem

I'm trying to accumulate formatted string in a variable. Something similar to: ``` for i in 1 2 3; do a="${a} `printf "%-10s %s" "hello" "world"`" done ``` However, when I echo the output, it doesn't preserve the format, even when I use the `-e` or `-n` flags along with the echo command. How should I do that? Thanks

Original source