如何将格式化的字符串回显到变量中

Ome*_*gan 0 bash

我正在尝试在变量中累积格式化的字符串.类似的东西:

for i in 1 2 3; do
    a="${a} `printf "%-10s %s" "hello" "world"`"
done
Run Code Online (Sandbox Code Playgroud)

但是,当我回显输出时,它不会保留格式,即使我使用-e-n标记以及echo命令.我该怎么办?

谢谢

fed*_*qui 5

回音时你引用了你的变量吗?如果这样做,您将看到格式保留.

$ for i in 1 2 3; do     a="${a} `printf "%-10s %s" "hello" "world"`"; done
$ echo "$a"
 hello      world hello      world hello      world
Run Code Online (Sandbox Code Playgroud)

虽然没有引用破坏格式的一切:

$ echo $a
hello world hello world hello world
Run Code Online (Sandbox Code Playgroud)