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?
-
3These are terminal sequences, not bash sequences. You need to look at your terminal documentation to determine their function.– Chris DownFeb 21, 2014 at 9:31
-
1Those sequences serve to colorize the output. Check: stackoverflow.com/questions/16845699/… and stackoverflow.com/questions/5947742/…– adizereFeb 21, 2014 at 9:32
-
1@adizere not all escape sequences perform color changes. The sequences in this Q are for line manipulation and cursor movement, not color.– caseyFeb 21, 2014 at 16:55
4 Answers
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
.
-
-
@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.– GraemeFeb 21, 2014 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 Feb 21, 2014 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 setxterm
into a mode that doesn't accept them. I will update with some more info on this I think.– GraemeFeb 21, 2014 at 10:48 -
1Anyone got an alternative to termsys.demon's site? It seems the link died sometime in 2020 (possibly a casualty of the pandemic?), So I'm falling back on web.archive.org's mirror for now, but I'd love to see this updated with a new link if anyone can find that does this one justice! Nov 14, 2021 at 17:19
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.
'\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.
-
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 getbash
to interpret escape codes directly, eg$'\033[999D'
– GraemeFeb 21, 2014 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 ;]