Jernej Jerin Jernej Jerin - 1 year ago 68
Linux Question

Using colors with printf

When written like this, it outputs text in blue:

printf "\e[1;34mThis is a blue text.\e[0m"

But I want to have format defined in printf:

printf '%-6s' "This is text"

Now I have tried several options how to add color, with no success:

printf '%-6s' "\e[1;34mThis is text\e[0m"

I even tried to add attribute code to format with no success.
This does not work and I can't find anywhere an example, where colors are added to printf, which has defined format as in my case.


You're mixing the parts together instead of separating them cleanly.

printf '\e[1;34m%-6s\e[m' "This is text"

Basically, put the fixed stuff in the format and the variable stuff in the parameters.