Bash tips: Colors and formatting (ANSI/VT100 Control sequences)
The ANSI/VT100 terminals and terminal emulators are not just able to display black and white text ; they can display colors and formatted texts thanks to escape sequences.
Those sequences are composed of the Escape character (often represented by “^[” or “<Esc>”) followed by some other characters: “<Esc>[FormatCodem”.
In Bash, the <Esc> character can be obtained with the following syntaxes:
\e
\033
\x1B
Examples:
Code (Bash) Preview
echo -e "\e[31mHello World\e[0m"
Hello World
echo -e "\033[31mHello\e[0m World"
Hello World
NOTE¹: The -e option of the echo command enable the parsing of the escape sequences.
NOTE²: The “\e[0m” sequence removes all attributes (formatting and colors). It can be a good idea to add it at the end of each colored text. ;)
NOTE³: The examples in this page are in Bash but the ANSI/VT100 escape sequences can be used in every programming languages.