Print color text in command line.
December 24th, 2006 mysurface Posted in Text Manipulation, clear, printf, read | Hits: 25059 |
Sometimes echo is not enough, if you need to print more advanced format of text. Lucky, we have printf. printf is a common function call in c programming language, if you learn c before, you should very familiar with this function.
Simply illustrate the power of printf, do as follow:
printf '\n\t\thello\tworld\n'
Besides readable character, printf can accept unreadable character too in octal value, hex value etc. That we can inject color setting to ask printf to print text in color.
printf '\x1b\x5b1;31;40m\tMerry Christmas\n'
\x1b\x5b is actually hex code 1B and 5B, this trailing is use to change the graphics display setting.
1;31;40m is actually the part of changing text attribute,fore color and background color.
But this leave a side effect, the settings stay after the printf. To return it back to normal, set back to foreground white and backgroud black.
printf '\x1b\x5b1;31;40m\tMerry Christmas\n\x1b\x5b0;37;40m'
P/S: For color code, refers to reference link.
This somehow leave it like a stain if your console is in pseudo transparent, to make it nice, I add more stuff in.
clear;printf '\x1b\x5b1;31;40m\tMerry Christmas\n\x1b\x5b0;37;40m';read;ls;clear
This is a stupid hack! You can actually do better. First I clear the screen, then print the stuff I want and set back the setting, wait for an user enter by using read, simply run ls ( this is to get back pseudo transparent at gnome-terminal) and at last clear screen again.
You can move your cursor before printing the text,
clear;printf '\x1b\x5b12;25f\x1b\x5b1;31;40m\tMerry Christmas\n\x1b\x5b0;37;40m';read;ls;clear
With the same hex code, add in 12;25f , 12 is the row, 25 is the column. I end the example here and wish you Merry Christmas, check out the reference for the trick!
Reference:
ANSI CODES
Live Chat!









January 20th, 2007 at 1:45 pm
[...] Reference: Print Color Text in Command Line ANSI CODES [...]
November 20th, 2007 at 2:02 am
[...] Text will be print in red, but color is not reset after that. For more examples on how to print color text, refers to this post. [...]
June 5th, 2008 at 8:51 am
[...] [...]
July 5th, 2008 at 12:59 pm
[...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]
September 5th, 2008 at 1:49 pm
joy comic nurse hentai joy nurse comic hentai
September 16th, 2008 at 7:51 pm
[...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]
September 23rd, 2008 at 11:08 am
[...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]