Linux Terminal character color settings

Source: Internet
Author: User

When we write the program, we usually need the terminal to display the output of red or green color, so that the user can be more clearly prompt. Here is how the Linux system terminal colors are set:

The character color of terminal is controlled by escape sequence, it is the system display function in text mode, and the specific language is independent, Shell,python,perl, C, C + + etc. can be called.
The escape sequence starts with the ESC and can do the same with \033 (the ASCII code for ESC is 27 in decimal notation, equivalent to 33 in octal notation).

Display format: \033[display mode; foreground color; background colour m
Where the display method, foreground color or background color is not in order, any of them is not required.
system default color: \033[0m

Display mode: 0 (default), 1 (highlighted), 22 (not bold), 4 (underscore), 24 (non-underlined), 5 (blinking), 25 (non-flashing), 7 (inverted), 27 (non-inverting)

Foreground color: 30 (Black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (magenta), 36 (cyan), 37 (white)

Background color: 40 (Black), 41 (red), 42 (green), 43 (yellow), 44 (blue), 45 (magenta), 46 (cyan), 47 (white)

Demonstration Source code:

[CPP]View Plaincopyprint?
  1. /******************************************************************************
  2. Copyright by Javacode007, all rights reserved!
  3. filename:setcolor.c
  4. author:javacode007
  5. date:2012-8-5
  6. version:1.0
  7. Description: Set Linux terminal character color
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. / * Set Terminal to system default display * /
  13. void Set_term_default ()
  14. {
  15. fprintf (stdout, "\033[0m");
  16. Fflush (stdout);
  17. }
  18. / * Set the terminal display mode * /
  19. void Set_term_color (const char* msg, const Char* fmt)
  20. {
  21. char *pfmt = NULL;
  22. PFMT = (char*) malloc (sizeof(FMT) + sizeof("\033[m"));
  23. if (NULL = = pfmt)
  24. {
  25. printf ("Malloc error! file:%s, line:%d\r\n ", __file__, __line__);
  26. return;
  27. }
  28. strcat (pfmt, "\033[");
  29. strcat (PFMT, FMT);
  30. strcat (pfmt, "M");
  31. printf ("Set Terminal Color to: \\033[%sm\r\n", FMT);
  32. fprintf (stdout, pfmt);
  33. printf (msg);
  34. Fflush (stdout);
  35. Free (PFMT);
  36. }
  37. int Main ()
  38. {
  39. / * Set the terminal font to be highlighted in red and the background color blue * /
  40. Set_term_color ("hello,world\r\n", "1;31;44");
  41. / * System default display for restoring terminal fonts * /
  42. Set_term_default ();
  43. printf ("hello,world\r\n");
  44. return 0;
  45. }
/******************************************************************************copyright by Javacode007, all Rights reserved! Filename:setcolor.cauthor:javacode007date:2012-8-5version:1.0description: Set Linux terminal character color ******* /#include <stdio.h> #include <    Stdlib.h> #include <string.h>/* set the terminal to the system default display */void Set_term_default () {fprintf (stdout, "\033[0m"); Fflush (stdout);}    /* Set terminal display mode */void set_term_color (const char* MSG, const char* FMT) {char *pfmt = NULL;        PFMT = (char*) malloc (sizeof (FMT) + sizeof ("\033[m")); if (NULL = = pfmt) {printf ("Malloc error!        file:%s, line:%d\r\n ", __file__, __line__);    Return    } strcat (Pfmt, "\033[");    strcat (PFMT, FMT);        strcat (Pfmt, "M");    printf ("Set Terminal Color to: \\033[%sm\r\n", FMT);    fprintf (stdout, pfmt);    printf (msg);        Fflush (stdout); Free (PFMT);} int main () {/* Sets the terminal font to be highlighted in red with a background color ofBlue */Set_term_color ("hello,world\r\n", "1;31;44");    /* system default display for restoring terminal fonts */Set_term_default ();    printf ("hello,world\r\n"); return 0;}


The output effect is as follows:

Linux Terminal character color settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.