Using colors with printf Using colors with printf bash bash

Using colors with printf


Rather than using archaic terminal codes, may I suggest the following alternative. Not only does it provide more readable code, but it also allows you to keep the color information separate from the format specifiers just as you originally intended.

blue=$(tput setaf 4)normal=$(tput sgr0)printf "%40s\n" "${blue}This text is blue${normal}"

See my answer HERE for additional colors


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.


This works for me:

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

From printf(1):

%b     ARGUMENT as a string with '\' escapes interpreted, except that octal       escapes are of the form \0 or \0NNN