I found many escape sequences in Bash starting with
\033], but what are these sequences and why are they starting with
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
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):
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
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
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
\033 as in your example.
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.
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
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'
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.
Escape followed by a right square bracket escape
] is used to introduce an operating system command (
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
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
PM which differed from
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 xterm
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
$ echo -n ".";tput setb 6;tput setf 4;echo -n "test";tput sgr0;echo "."
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< 0000006
... where the values are show in hexadecimal (due to "x").