LS_COLORS for 256-color terminal

colorsescape-characterslsterminal

I thought ls colors are defined as

<file_type>=[<bg(40-47)>];<font_spec(0:5)>;<font_color(30-37)>

but, I recently found this where there are more colors and colors are specified as, e.g.:

.tar    00;38;5;61

for a 256-color terminal.

What does this definition mean?

Best Answer

The argument to a LS_COLORS directive is a string that is written to the terminal as part of an escape sequence. When displaying a file name, ls writes \e[, then the string associated with the file type, then m, then the file name, then \e[0m (where \e represents an escape character). This is the escape sequence that tells xterm and compatible terminals (which is most of them nowadays) to change colors and other text attributes (CSI Pm m in the documentation. ls doesn't care what the sequence of characters means or how many semicolons it contains.

Old terminals only supported 8 foreground colors, designated by the numbers 30 through 37. Terminals that support more colors use the escape sequence \e[38;5;Psm where Ps is a color number, or \e[38;2;Pr;Pg;Pbm where Pr, Pg, Pb are RGB values. These can be combined with other attributes, e.g. \e38;5;61;1m or \e38;2;95;95;175;1m for bold slate blue text.

Related Question