Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 texte[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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
496 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
" "${blue}This text is blue${normal}"

See my answer HERE for additional colors


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...