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 have come across bash sequences such as \033[999D and \033[2K\r which are used to do some manipulation on a printout on a terminal. But what do these sequences mean? Where can I find a list/summary on the web to help me find out the meaning of these sequences?

share|improve this question
3  
These are terminal sequences, not bash sequences. You need to look at your terminal documentation to determine their function. –  Chris Down Feb 21 at 9:31
1  
Those sequences serve to colorize the output. Check: stackoverflow.com/questions/16845699/… and stackoverflow.com/questions/5947742/… –  adizere Feb 21 at 9:32
    
@adizere not all escape sequences perform color changes. The sequences in this Q are for line manipulation and cursor movement, not color. –  casey Feb 21 at 16:55

4 Answers 4

up vote 20 down vote accepted

See this link http://www.termsys.demon.co.uk/vtansi.htm. As Anthon says, \033 is the C-style octal code for an escape character. The [999D moves the cursor back 999 columns, presumably a brute force way of getting to the start of the line. [2K erases the current line. \r is a carriage return which will move the cursor back to the start of the current line and is a C-style escape sequence rather than a terminal control sequence.

Update

As other people have pointed out, these control sequences are nothing to do bash itself but rather the terminal device/emulator the text appears on. Once upon a time it was common for these sequences to be interpreted by a completely different piece of hardware. Originally, each one would respond to completely different sets of codes. To deal with this the termcap and terminfo libraries where used to write code compatible with multiple terminals. The tput command is an interface to the terminfo library (termcap support can also be compiled in) and is a more robust way to create compatible sequences.

That said, there is also the ANSI X3.64 or ECMA-48 standard. Any modern terminal implementation will use this. terminfo and termcap are still relevant as the implementation may be incomplete or include non standard extensions, however for most purposes it is safe to assume that common ANSI sequences will work.

The xterm FAQ provides some interesting information on differences between modern terminal emulators (many just try to emulate xterm itself) and how xterm sequences relate to the VT100 terminals mentioned in the above link. It also provides a definitive list of xterm control sequences.

Also commonly used of course is the Linux console, a definitive list of control sequences for it can be found in man console_codes, along with a comparison to xterm.

share|improve this answer
    
The link you included is just the page I was looking for. –  Alex Feb 21 at 9:37
    
@Alex Glad I found it myself, most pages I've seen only cover colours and somehow are really complex to read. This lays it out nice and simple. –  Graeme Feb 21 at 9:46
    
be careful that the action of escape codes depends on the type of terminal (and you can emulate several types of terminal on modern screens/OS). You can use tput commands instead, to have more portability, iirc (but at the expense of less coverage of the specifics of your terminal). VT100 (the one that page talks about) is supported by many (but not everything is supported, I believe...). But your TERM variable may be set to some other terminal, and this could affect the effects of some of the commands –  Olivier Dulac Feb 21 at 10:20
    
@Olivier, correct tput is a more robust way. But it is the ANSI compatibility rather than the specific VT100 terminal that is now fairly ubiquitous, although I imagine it is possible to set xterm into a mode that doesn't accept them. I will update with some more info on this I think. –  Graeme Feb 21 at 10:48
    
@Olivier A lot of terminal emulators also set TERM=xterm to but don't implement everything xterm does. –  Graeme Feb 21 at 17:29

The \033 is the escape character, and those sequence are not bash specific but interpreted by the terminal (software or hardware (via network or serial line)) in which the (bash) program runs. There are many such sequences.

What each does, depends on the terminal for which it is dependent, and might also depend on any previous sequence altering the state of the terminal.

These are often used to set colors in the bash prompt, but I did not find your particular examples on that page.

share|improve this answer

'\033[999D' is funny. Some terminals implement fewer of the escape sequences, so this might be needed if none of '\033[1G', '\033[1;1H', '\033[1;1f', or '\r' behave as they should, but who uses a thousand-column text display? These sequences have a man page in Linux: console_codes(4). bjh21 has a detailed list of all the codes. Depending on which program parses '\033', the more clear substitution '\e' might be available.

It is really not intended that these be used directly — the correct way to issue terminal control commands is with an abstraction layer such as ncurses — but they function just as well from string literals.

share|improve this answer
    
Good point, I just assumed that \033[999D would back up over the previous line(s). May just be the result of dodgy coding though ;) You can also get bash to interpret escape codes directly, eg $'\033[999D' –  Graeme Feb 21 at 17:18

you can search for "033 term escape list" or "033 tput" and see http://wiki.bash-hackers.org/scripting/terminalcodes for more info before someone answering ;]

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.