I found many escape sequences in Bash starting with \033], but what are these sequences and why are they starting with \033]?


The string is actually \033[ and that's not the whole thing.

After that opening bracket comes a series of numbers and symbols. This string is known as an escape sequence and is used to control the console's cursor and text color, among other things.

non-printing escape sequences have to be enclosed in \[\033[ and \]

If the escape sequence is controlling text color, then it will be followed by an m.

Here's a table for the color sequences:

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   

So, if you want your console prompt to be blue, you would use the following escape sequence (in the filename I'm forgetting):


(Notice the m)

This escape sequence doesn't only control color, however. It can also control cursor movement. Here's a table/list with the movement codes and how they work:

  • Position the Cursor:




    puts the cursor at line L and column C.

  • Move the cursor up N lines:

  • Move the cursor down N lines:

  • Move the cursor forward N columns:

  • Move the cursor backward N columns:

  • Clear the screen, move to (0,0):

  • Erase to end of line:

  • Save cursor position:

  • Restore cursor position:


Just be aware that the last two may not work in the terminal emulator you use. Apparently, only xterm and nxterm use those two sequences.

And example using one of these escape sequences: say I want to position my cursor at line 3, column (character) 9. For that, I would use


(I am assuming that column 0 is the first position, so that would be the 8th character).

Source: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html (also read 6.2)

More general reading: http://ascii-table.com/ansi-escape-sequences.php

Wikipedia: https://en.wikipedia.org/wiki/ANSI_escape_code

| improve this answer | |
  • 2
    consider also linking to en.wikipedia.org/wiki/ANSI_escape_code – cat Oct 1 '16 at 16:36
  • 3
    Note that '\033' is the ESCAPE character in octal, so it could be given in decimal (27) or hex (0x1B). There are other common ways of writing ESCAPE, e.g. '^[' in shell scripts, "\e" in bindkey, etc. – jamesqf Oct 1 '16 at 19:01
  • 1
    Please note that a number of entries in that table are misleading or inaccurate: bold, faint, underlining etc. can be set and reset independently of color, and are also reset by 0 along with the colors; there's a whole separate set of codes (40–49) for background, and 7 instead swaps foreground and background; ESC[2K erases the entire line both before and after the cursor. A much better source would be the Wikipedia article, or you could go directly to ECMA-48, section 8.3.117 SGR – Select Graphic Rendition. – deltab Oct 2 '16 at 3:21
  • The phrase "'m' character at the end of each of the following sentences is used as a stop character" on the cplusplus.com post is also a bit confusing, since the table just below contains commands that obviously aren't terminated by an "m". Actually it seems even more misleading here when presented right at the top. – ilkkachu Oct 2 '16 at 7:43
  • 1
    Technically, the leading 0; in the color sequences is unnecessary. That just makes the background color the default before setting the foreground. You can also set the background with 4x. – Fund Monica's Lawsuit Oct 2 '16 at 15:31

They're control commands for the terminal.

Terminals were originally rather dumb devices connected to a serial port, and not those fancy multi-tab GUI software with menus we now have. Since they only received a stream of characters (bytes), there was no clear separation between data (what to print on the screen) and commands (how to print it). Instead the commands are represented with special control characters.

The obvious control characters are stuff like line feed (newline), backspace and bell beep, but more specific commands are given as sequences of characters, starting with the ESC character (code 27 in decimal, 0x1b in hex, or 033 octal). It's often represented as ^[, or \033 as in your example.

The sequence ESC [ is called the CSI, or Control sequence introducer, and it starts a command with optional numeric parameters, ending in usually a letter that defines the main command. Most of the common escape sequences fall in this class.

Lists of the escape codes can be found e.g. in the console_codes(4) man page, and on the Wikipedia page for ANSI escape codes.

Some examples:

ESC [ 4 A             move cursor 4 lines up (4 can be any number)
ESC [ 5 B             move cursor 5 lines down
ESC [ 2 K             erase current line 
ESC [ 30;46 m         set black text (30) on cyan background (46)
ESC [ 0 m             reset color and attributes

You can test the commands with e.g. Bash. Using the -e flag, the builtin echo command accepts \033 as a representation of the ESC character.

E.g. this will print a greeting in color in the middle of the screen and another normally in the original cursor position:

echo -e '\033[s\033[12;30f\033[30;46m  Hello!  \033[0m\033[uhello'

The sequence ESC ] which you mentioned is the OSC or Operating System Command, which is mostly used in the command to set the window title in xterm and others, e.g.:

echo -e '\033]0;new window title\a'

Then there's also ESC ( A (and other letters) that set national character sets on some terminals, to a potentially hilarious effect.

| improve this answer | |

Escape followed by a right square bracket escape] is used to introduce an operating system command (OSC).

It is in ECMA-48, and you can read a summary of the control sequences used by xterm in XTerm Control Sequences, e.g., for changing the title of the terminal window.

Escape sequences can begin with different characters. The C1 (8-Bit) Control Characters section in XTerm Control Sequences mentions a few of those: CSI (which you see as escape[, APC, DCS, PM. The reason for the different (second) character is because the pair (escape and ]) is associated with a single-byte control character used for different types of escape sequence.

If you read through the specification, you will notice that CSI is used for controls with numeric parameters, while OSC allows strings. Beyond just the syntax, the committee which created this standard had in mind uses for APC and PM which differed from DCS and OSC.

| improve this answer | |

These are called ANSI escape codes, and they are listed in the man page for console_codes. They are not Bash-specific, but rather work in any console application as long as the terminal supports them (most terminal emulators do).

| improve this answer | |

The "escape sequences" described in the other answers are dependent on which terminal TYPE you have (most often "ANSI" or derivates/similar ones).

$ echo $TERM  

If you wish to write scripts that are (somewhat) INDEPENDENT of the termninal, but still uses e.g. color and other special features, then have a look on the content of man terminfo.


$ echo -n ".";tput setb 6;tput setf 4;echo -n "test";tput sgr0;echo "."

will print .test. with test having yellowish background and red foreground color, and this will work for ANY terminal that actually is capable of using colors in the same manner.

To see what is actually printed, pipe it into od -t x1z as in

$ tput sgr0 | od -t x1z  
0000000 1b 28 42 1b 5b 6d                                >.(B.[m<  

... where the values are show in hexadecimal (due to "x").

| improve this answer | |

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.