Ansi Escape Sequences in Shell Scripts
Contents
1. Introduction
Common terminal emulaters such as xterm, rxvt, PuTTY, and the Default Terminal emulators of the
UNIX/Linux desktop environments all support text colour and shape manipulation based on escape sequences.
This small article descripes how to make use of this capability in shell scripts (e.g. red or blinking
text for warning messages or other colours for status messages).
2. List of ANSI Codes
This is a selective list of ANSI escape sequences and some more special characters useful for shell scripts:
::: Text Colours ::: Black ESC[30m Red ESC[31m Green ESC[32m Yellow ESC[33m Blue ESC[34m Magenta ESC[35m Cyan ESC[36m White ESC[37m Default ESC[39m ::: Background Colours ::: Black ESC[40m Red ESC[41m Green ESC[42m Yellow ESC[43m Blue ESC[44m Magenta ESC[45m Cyan ESC[46m White ESC[47m Default ESC[49m ::: Text Properties ::: Bold (preserve colours)............ ESC[1m Underlined text.................... ESC[4m Blinking text...................... ESC[5m Reset everything to default........ ESC[0m ::: Miscellaneous ::: Window Title....................... ESC]0;${TITLE}BEL Terminal Bell...................... BEL Switch to graphics character set... ^N Switch to normal character set..... ^O ::: Examples ::: Bold yellow text on red background. ESC[1;33;41m Bold underlined text............... ESC[1;4m _____________________________________________ Legend: - ESC = ^[ = \033 - BEL = ^G = \007 - ^N = \016 - ^O = \017
Multiple escape sequences can be combined.
Example: We want to combine bold text shape, yellow text colour and red background.
ESC[1m
ESC[33m
ESC[41m
The result is ESC[1;33;41m
- the escape sequence we use in our examples in
the next paragraph.
3. How to Use ANSI Codes in Shell Scripts
Use with »printf«
The codes from the list above can be used with printf on all UNIX systems. To print the line "This is bold yellow text on red background" in bold yellow text on red background with printf type:
$ printf "\033[1;33;41m
This is bold yellow text on red background\033[0m
\n"
Of course you can use variables here as usual:
$ TEXT="This is bold yellow text on red background" $ printf "\033[1;33;41m
%s\033[0m
\n" "$TEXT"
Use with »echo«
On many systems ANSI escape sequences can also be used with echo - depending on system and shell type:
$ echo -e "\033[1;33;41m
This is bold yellow text on red background\033[0m
\n"
Note the option "-e". Not all versions of echo support this options and would literally print "-e" instead.
Use with other commands
If you want to use ANSI codes with commands not natively supporting ANSI escape sequences it's entirely possible. Since all these sequences are interpreted by the terminal and not by the program you need to replace \033 by the ESCAPE character itself. If you use the vi editor type Ctrl-V ESC in editing mode and you get the ESCAPE character. You will see something like ^[. If you move with the cursor over it you will see it's only one character. If you need the BEL character (\007) you have to use Ctrl-G (in vi type: Ctrl-V Ctrl-G and you see something like ^G.
Examples:
$ BOLDTEXT="^[
[1;33;41m This is bold yellow text on red background^[
[0m" $ echo "$BOLDTEXT" $ $ TITLE="^[
]0; This is the new window title^G
" $ echo "$TITLE"