Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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
    

4 Answers 4

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.

share|improve this answer

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.

share|improve this answer

\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).

share|improve this answer

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..)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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