the ANSI escape code `ESC[1;31m` sets terminal foreground color as red, what does `1` mean there?

Solution 1:

That's documented in another table two paragraphs up.

The general rule is that ESC [ ... m consists of a list of individual SGR formatting codes. The first parameter has no special meaning – 1 is simply the formatting code for "bold" or "bright" text. (It is up to terminal whether to make the text bold, or bright, or both.)

As a more complex example, ESC[;1;3;31;47m would first reset formatting (0), then make the text bold (1) and italic (3), then set the foreground to red (31) and background to white-ish (47).


Exceptions to the general rule:

Note that the "de facto" accepted 256-color and true-color code formats violate this syntax. For example, the correct way to specify color #42 is ESC[38:5:42m (with "38:5:42" being a single formatting code), but is commonly written as ESC[38;5;42m (as if those were 3 separate codes), and terminals often have special handling to re-combine the 3 parameters.

Similarly, 24-bit or true-color codes are officially written as ESC[38:2::R:G:Bm (single code with many subfields), yet often you'll see ESC[38;2;R;G;Bm instead. This again has special handling in terminals as the 5 separate "codes" are re-combined into a single truecolor formatting code.

Solution 2:

I'd like to amend user1686's excellent answer with a side note.

Traditionally, terminals have supported 16 colors only (8 basic colors and their brighter counterparts). Later 256 color support emerged and became widespread, nowadays almost every terminal emulator supports them. In the last couple of years the truecolor (16M colors) extension became quite widespread, too. However, ESC[1m still only affected the brightness of the first 8 palette colors, and only the foreground color. (It definitely wouldn't have made sense to alter the direct RGB truecolor values.)

In the mean time, while there are standalone and unambiguous codes for other typefaces or decorations (like 3 for italic, 4 for underlined etc.), there is no way to enable bold typeface without side effects.

In order to clean up this legacy and be able to cleanly move forward, several terminal emulators deliberately decided to let ESC[1m switch to bold typeface only, without altering the color. Some made this their default settings while allow you to switch back to the legacy behavior, some only support this bold-only behavior.

I expect more and more terminal emulators to follow this path in the future.