3

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

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.