Escape sequences (some defined set of characters) with ANSI color codes are used to change the color of the output on a Linux Terminal.
The string preceded with the escape sequence to be colored in Linux Terminal.
$ echo -e "\e[1;31mHai Everybody!"
The flag '-e' will enable the echo command to consider the escape sequences in the string. Without the flag, an echo will print the characters \e[1;31m like this.
We can also use multiple colors for different substrings.
$ echo -e "\e[1;32mHai \e[1;35mEverybody!"
The colors in the range 30-39 are for the text. If we want to change the background color, we can do so by using the colors of codes 40 and above.
$ echo -e "\e[1;46mHai Everybody!"
We can see that the background color Cyan has not only been applied on the text, but also on the console prompt string. To prevent this, end our echo string with an escape string with no color code.
$ echo -e "\e[1;46mHai Everybody!\e[1;m"
We can use many more colors for our string. The list of colors is as follows:
Color Text Background
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47
Using the python script below, you are able view the color codes on the console page,
Using the python script below, you are able view the color codes on the console page,
$ python -c "print('\t'.join(f'\u001b[1;38;5;{color}m{color.ljust(4)}' + ('\n' if not int(color) % 8 else '') for color in (str(i) for i in range(256))) + '\u001b[0m')"
Comments (0)