Print color text in command line.

December 24th, 2006 mysurface Posted in clear, printf, read, Text Manipulation | Hits: 155004 | 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

19 Responses to “Print color text in command line.”

  1. [...] Reference: Print Color Text in Command Line ANSI CODES [...]

  2. [...] 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. [...]

  3. [...] [...]

  4. [...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]

  5. joy comic nurse hentai joy nurse comic hentai

  6. [...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]

  7. [...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]

  8. 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

  9. echo -e “33[32m[ Successful ] ### Girya ###33[0m”

  10. kewl info… really helpful.

  11. Oscillator Says:

    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

  12. Great blog

  13. 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.

  14. Firefox wont start but when I open a link that opens firefox, It works fine. What do I do?

  15. [...] Print Color Text in Command Line ANSI [...]

  16. [...] Now, run ./clua again! You name will be in RED, check out text color example HERE [...]

  17. Please message me with a few hints & tips about how you made this blog look like this , Id be appreciative!

  18. [...] 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 http://linux.byexamples.com/archives…-command-line/ [...]

  19. 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.

Leave a Reply