ANSI Escape Sequences
ANSI Escape sequences are some standardised byte sequences that make most Terminals do some thing other than just printing characters on screen. Like printing colourful characters!
Table of Contents
Using ANSI-Escape Sequences
ANSI-Escape sequences can be used by writing an ASCII-Escape characters followed by some instructions to your Programs standard output or standard error.
The codes can be produced by any string escaping mechanism. In proper programming and scripting languages the syntax for the escape character in double quoted strings is usually \x1b
.
The shell being a bit different you should use either printf
or echo -e
. While most systems support both the chance of printf
being supported is usually higher.
Using ANSI-Escape Sequences Responsibly
While colors and text effects are pretty: You should consider the cases where they shouldn't be enabled and respect user choices.
- Disable color output by default when the
NO_COLOR
environment variable is non-empty. - Disable use of escape codes by default when the output isn't a terminal.
- Don't overuse color, it gets confusing pretty quickly.
- Make sure no information gets lost when disabling escape codes.
- Does using grep on the output work to find information?
- Does piping it into
espeak-ng
give a sensible result?
When writing by default
that means unless explicitly requested (i.e. through a --color always
CLI option).
Thank you to Seirdy! Most of the above is a summarized and filtered TL;DR of Seirdys article, you should definitely read it!
Setting Text Color and Effects
I'll focus on Select Graphic Rendition (SGR) codes here, Wikipedia has a more complete list.
As you can see in the example, multiple escape-codes can be chained in one sequence by delimiting them with a ;
.
The \033
part will turn into an ASCII escape character, the [
tells the terminal that this is an ANSI Control Sequence Initiator (CSI) after that can come one or more codes. The sequence is finished by an m
to tell the terminal that everything between the CSI and it are SGR codes.
Note on the \033
and compatibility: The \033
is the octal representation of the Escape ASCII-Character and is the most widely supported one across printf
implementations. (The dash printf implementation for example doesn't support hexadecimal representation.)
Code | Code to reverse | Effect |
---|---|---|
00 | Reset all effects | |
Foreground color | ||
30-37 | 39 | Set the Foreground color |
90-97 | 39 | Set the Foreground color, bright variation |
Background color | ||
40-47 | 49 | Set the Background color |
100-107 | 49 | Set the Background color, bright variation |
Font effects | ||
01 | 22 | Bright text (usually bold and brighter color) |
02 | 22 | Dim text (usually only effect on color) |
03 | 23 | Italic text |
04 | 24 | Underlined text |
05 | 25 | Blinking text |
07 | 27 | Reverse foreground and background color |
08 | 28 | Concealed/Hidden text |
09 | 29 | Strikethrough text |
Note: The leading zeros are not needed.
Colors
You may have noticed that the 3x
, 4x
, 9x
, and 10x
family of codes all use the last digit in the range from 0 to 7 as a color parameter.
Code | Color |
---|---|
0 | Black or Dark Grey |
1 | Red |
2 | Green |
3 | Yellow |
4 | Blue |
5 | Violet / Purple |
6 | Cyan or Turquoise |
7 | Light Grey or White |
Please keep in mind that different people have different color palettes when you want to write reusable scripts.