Shell text color control from C program

How to control output text color of a shell?

Text color output is not defined in ANSI C/C++. Instead the creators of the language left that to be operating system dependent. In Linux, to change text color you must issue what are known as terminal commands.

To print "Hello, world.\!" in red, change the printf statement to read:printf("\033[22;31mHello, world!");

\033[22;31m is the terminal command. Note the numneric escape sequence at the start and on an ASCII based machine, octal 33 is the ESC (escape) code. For further details on numneric escape sequence read http://casual-pavan.blogspot.com/2008/10/intersting-things-about-escape.html

To try different colors, just change the numbers after the left bracket.
\033[22;30m is black
\033[22;31m is red
\033[22;32m is green

Some more colours:
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m - white

Check the Terminal HowTo for a better reference.

Comments

Popular posts from this blog

shriranga kamalakanta

Deva Gharache dnyat kunala

Hindu aikya ghosh