Print color text in command line.
December 24th, 2006 mysurface Posted in clear, printf, read, Text Manipulation | Hits: 219121 | 19 Comments »
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
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 […]
July 31st, 2009 at 4:02 pm
hey guys , gimme 5 min, and lets have a look on the belows examples…….
==============In Case Of Java: ===================
public class Girish
{
public static void main (String args[])
{
printExceptionMessage(\"This is the exception message\");
printInfoMessage(\"This is the info message\");
}
public void printExceptionMessage(String excep_message)
{
System.out.println(\"\33[31m\"+excep_message+\"\33[0m\");
}
public void printInfoMessage(String info_message)
{
System.out.println(\"\33[34m\"+info_message+\"\33[0m\"+\"\\n\");
}
}
======================Linux========================
Color for Echo: Print terminal output in Color………
Black: \33[30m
Red: \33[31m
Green: \33[32m
Yellow: \33[33m
Blue: \33[34m
Magenta: \33[35m
Cyan: \33[36m
White: \33[37m
echo -e \"\33[32m##### Girya ############\33[0m\"
Regards,
Girish Mahajan
July 31st, 2009 at 4:07 pm
echo -e “33[32m[ Successful ] ### Girya ###33[0m”
August 11th, 2009 at 5:52 pm
kewl info… really helpful.
May 9th, 2010 at 12:49 pm
I think this may be helpful for more than one.
I have this as header on all my programs:
#ifdef USECOLORS
#define BLACK \"\33[22;30m\"
#define GRAY \"\33[01;30m\"
#define RED \"\33[22;31m\"
#define LRED \"\33[01;21m\"
#define GREEN \"\33[22;32m\"
#define LGREEN \"\33[01;32m\"
#define BLUE \"\33[22;34m\"
#define LBLUE \"\33[01;34m\"
#define BROWN \"\33[22;33m\"
#define YELLOW \"\33[01;33m\"
#define CYAN \"\33[22;36m\"
#define LCYAN \"\33[22;36m\"
#define MAGENTA \"\33[22;35m\"
#define LMAGENTA \"\33[01;35m\"
#define NC \"\33[0m\"
#define BOLD \"\33[1m\"
#define ULINE \"\33[4m\" //underline
#define BLINK \"\33[5m\"
#define INVERT \"\33[7m\"
#else
#define BLACK \"\"
#define GRAY \"\"
#define RED \"\"
#define LRED \"\"
#define GREEN \"\"
#define LGREEN \"\"
#define BLUE \"\"
#define LBLUE \"\"
#define BROWN \"\"
#define YELLOW \"\"
#define CYAN \"\"
#define LCYAN \"\"
#define MAGENTA \"\"
#define LMAGENTA \"\"
#define NC \"\"
#define BOLD \"\"
#define ULINE \"\"
#define BLINK \"\"
#define INVERT \"\"
#endif
Usage: printf(RED\"Hello, I\’m process %i\\n\"NC,getpid()); //NC stands for no color. Returns to console default configurations
May 19th, 2011 at 1:18 am
Great blog
August 6th, 2011 at 2:31 pm
I do enjoy the manner in which you have framed this issue and it really does provide me a lot of fodder for consideration. Nonetheless, through everything that I have observed, I basically trust as the actual commentary stack on that folks continue to be on point and in no way start upon a tirade of the news du jour. Anyway, thank you for this excellent piece and whilst I do not really go along with it in totality, I value the point of view.
August 24th, 2011 at 4:43 pm
Firefox wont start but when I open a link that opens firefox, It works fine. What do I do?
December 7th, 2011 at 5:07 pm
[…] Print Color Text in Command Line ANSI […]
February 23rd, 2012 at 12:49 am
[…] Now, run ./clua again! You name will be in RED, check out text color example HERE […]
May 28th, 2012 at 5:02 pm
Please message me with a few hints & tips about how you made this blog look like this , Id be appreciative!
June 1st, 2012 at 2:09 am
[…] Originally Posted by fatherwillow How can I change the font color of the command line? The first two answers relate to changing the color of your prompt, but not necessarily that of the text printed in the terminal. For that I googled <linux command line text color> and this is the first result https://linux.byexamples.com/archives…-command-line/ […]
July 12th, 2013 at 8:09 am
I’m commenting to make you be aware of of the incredible encounter my wife’s girl encountered going through your web page. She learned a good number of things, which included what it is like to have an excellent teaching heart to have most people really easily comprehend selected very confusing subject areas. You undoubtedly surpassed readers’ expected results. Thanks for displaying the interesting, healthy, revealing and easy tips about the topic to Julie.