
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Color | Foreground Code | Background Code |
---|---|---|
Black | 30 | 40 |
Red | 31 | 41 |
Green | 32 | 42 |
Yellow | 33 | 43 |
Blue | 34 | 44 |
Magenta | 35 | 45 |
Cyan | 36 | 46 |
White | 37 | 47 |
Some additional options are like below −
Option | Code | Description |
---|---|---|
Reset | 0 | Back to normal (remove all styles) |
Bold | 1 | Bold the text |
Underline | 4 | Underline text |
Inverse | 7 | Interchange colors of background and foreground |
Bold off | 21 | Normal from bold |
Underline off | 24 | Normal from Underline |
Inverse off | 27 | Reverse 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
- Related Articles
- How to Change Terminal Output Color in Linux?
- Formatted text in Linux Terminal using Python
- How to Record Linux Terminal Sessions?
- How to Clear Linux terminal screen?
- How to download a website page on Linux terminal?
- How to View Colored Man Pages in Linux?
- How to use rainbow colors in linux terminal
- How to decorate your Linux Terminal using Shell?
- How to do simple Arithmetic on Linux Terminal?
- How to exit from a Linux terminal if a command failed?
- How to Save Command Output to a File in Linux?
- How to Test your Broadband Speed from Linux Terminal
- How to Find Linux Server Geographic Location in Terminal?
- How to create a new directory in Linux using the terminal?
- How to Create a File in the Linux/Unix system using terminal?

Advertisements