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!
The codes can be produced by any string escaping mechanism. In proper programming and scripting languages the syntax for double quotes is usually the same as for printf.
The shell being a bit different you should use either
echo -e. I have a personal preference for
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
\x1b part is an ASCII escape character it can also be written as
[ 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.
If you are using escape sequences in your scripts: Please make sure that no information gets lost when those sequences are stripped, People with processing pipelines and screenreaders will thank (or at least not curse at) you.
|Code||Code to reverse||Effect|
|00||Reset all effects|
|30-37||39||Set the Foreground color|
|90-97||39||Set the Foreground color, bright variation|
|40-37||49||Set the Background color|
|100-107||49||Set the Background color, bright variation|
|01||22||Bright text (usually bold and brighter color)|
|02||22||Dim text (usually only effect on color)|
|07||27||Reverse forground and background color|
Note: The leading zeros are not needed.
You may have noticed that the
10x family of codes all use the last digit in the range from 0 to 7 as a color parameter.
|0||Black or Dark Grey|
|5||Violet / Purple|
|6||Cyan or Turquoise|
|7||Light Grey or White|
Please keep in mind that diffrent people have diffrent color palettes when you want to write reusable scripts.