What type of sequences are escape sequences starting with "\033]"

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


Solution 1:

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

\[\033[34m\]

(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:

    \033[<L>;<C>H
    

    Or

    \033[<L>;<C>f
    

    puts the cursor at line L and column C.

  • Move the cursor up N lines:

    \033[<N>A
    
  • Move the cursor down N lines:

    \033[<N>B
    
  • Move the cursor forward N columns:

    \033[<N>C
    
  • Move the cursor backward N columns:

    \033[<N>D
    
  • Clear the screen, move to (0,0):

    \033[2J
    
  • Erase to end of line:

    \033[K
    
  • Save cursor position:

    \033[s
    
  • Restore cursor position:

    \033[u
    

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

\[033\[3;9H]

(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

Solution 2:

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.

Solution 3:

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.