ANSI Escape Sequences

Date: , Updated: — Topic: — by Slatian

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!

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.

When writing by default that means unless explicitly requested (i.e. through a --color always CLI option).

Seirdy about Best practices for inclusive CLIs.

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.

An example for the shell which prints out _FOOBAR_ in bright (01) red (31). After that, it resets to default settings (00) and prints a newline character (\n)
printf '\033[01;31m_FOOBAR_\033[00m\n'
# With %s placeholder
printf '\033[01;31m%s\033[00m\n' "_FOOBAR_"

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

Most commonly used codes
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.

Table of colors
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.