How to output colored text to a Linux terminal?


Here we will see how to print some lines into the linux terminal with some color. Here we are doing anything special into C++ code. We are just using some linux terminal commands to do this. The command for this kind of output is like below.

\033[1;31m Sample Text \033[0m

There are some codes for text styles and colors. These are listed below.

ColorForeground CodeBackground Code
Black
3040
Red
3141
Green
3242
Yellow
3343
Blue
3444
Magenta
3545
Cyan
3646
White
3747

Some additional options are like below −

OptionCodeDescription
Reset
0Back to normal (remove all styles)
Bold
1Bold the text
Underline
4Underline text
Inverse
7Interchange colors of background and foreground
Bold off
21Normal from bold
Underline off
24Normal from Underline
Inverse off
27Reverse of the Inverse

Example

#include<iostream>
using namespace std;
main() {
   cout << "\033[1;31mThis is bold red text\033[0m\n";
   cout << "\033[;32mGreen Text\033[0m\n";
   cout << "\033[4;33mYellow underlined text\033[0m\n";
   cout << "\033[;34mBlue text\033[0m\n";
}

Output

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements