25

I know that for printing a colored text using echo, for example red color, the code is: echo -e "\e[1;31m This is red text \e[0m"
and I know that in this example, 31 is code of red color and the number of other colors is:

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37

and for background colors, the num is 40 to 47

my Question is this: what does \e and [ and m(after 31) exactly mean here?
I read in the man page that \e is escape, but I didn't understand what it means.

  • In general, you escape characters that have special meanings in the current technical context. For example, strings are often written within quotes, "like this". But if you want quotation marks within a string, you must escape those chars so they will be interpreted literally and not as delimiters. For example, echo "She said \"Hi\"" As for this case (your question), you'll have to wait for a better answer. – Emanuel Berg Jul 18 '12 at 19:54
13

They are part of the 'Sequence elements' of ANSI escape sequences also known as ECMA-48 CSI sequences which were originally adopted in 1976

More specifically the [ is the 'Control Sequence Introducer'

Type man console_codes in almost any *nix distro to see the codes and some docs.

  • man 4 console_code not man console_code : In my ubuntu – 0x6773 Jun 26 '15 at 5:32
4

To be precise color codes are defined as below-

Opening
RED= "\033[31m" or "\e[1;31m"
CYAN="\033[36m" or "\e[1;31m"

Closing = "\033[m" or "\e[0m"

So basically its just the way different terminal interpret color codes and you caption your text within these elements. Its an odd way to make the terminal understand what you mean (like HexCodes or Binary..)

2

I found a nice explanation here (at least, nice for me :-) ). I'm not sure if it'll be useful for you.

To start, type this on your console:

echo "^[[0;31;40mIn Color"

The first character is an escape character, which looks like two characters, ^ and [. To be able to print this, you have to press CTRL+V and then the ESC key. All the other characters are normal printable characters, so you see the string In Color in red. The type stays that color until you revery back by typing this:

They are escape codes to set the color modes in the terminal. Is just that when printed with regular characters, they look like that.

2

\e is shorthand for the Escape character. the <ESC>[ and the m are the markers for the beginning and end of ANSI color control sequences, which are a subset of ANSI terminal control sequenced. Most of them start with <ESC>[, followed by a number, followed by a letter indicating which function is being implemented (e. g. move cursor to line; clear to end of line, return cursor to saved location, et cetera).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.