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!

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.

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
00Reset all effects
Foreground color
30-3739Set the Foreground color
90-9739Set the Foreground color, bright variation
Background color
40-4749Set the Background color
100-10749Set the Background color, bright variation
Font effects
0122Bright text (usually bold and brighter color)
0222Dim text (usually only effect on color)
0323Italic text
0424Underlined text
0525Blinking text
0727Reverse foreground and background color
0828Concealed/Hidden text
0929Strikethrough 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
CodeColor
0Black or Dark Grey
1Red
2Green
3Yellow
4Blue
5Violet / Purple
6Cyan or Turquoise
7Light Grey or White

Please keep in mind that different people have different color palettes when you want to write reusable scripts.