Terminal Colors – Is There a Manual Page for Colored Shell Output?

colorsdocumentationterminal

You know, that colorized output made by special char sequences, for example:

echo -e "\e[34m Hello\n \e[0m"

This will produce word "Hello" colored in blue. To do this I had to search on the web, and it is hard, because different sources may say something different.
I thought, that there must be some manuals, but I couldn't find them. Even with apropos color or something similar.
So, the question is – where I can find reliable manuals?

Best Answer

There are too many sources of information to list in this page:

  • The command echo -e uses an extension to render \e as ASCII 27 (octal 33 or "\033"). The portable way to print these uses printf (POSIX). Compare with standard echo (POSIX).
  • Your example can be rewritten as

    printf "\033[34m Hello\n \033[0m"

  • the standard for the color escapes is ECMA-48 (Control Functions for Coded Character Sets).
  • In ECMA-48, 8.3.117 SGR - SELECT GRAPHIC RENDITION lists control sequences for setting 8 foreground and 8 background colors.
  • It also lists codes 39/49 "default display colour (implementation-defined)" and 38/48 "reserved for future standardization; intended for setting character foreground colour as specified in ISO 8613-6 [CCITT Recommendation T.416]". That is, there is a recommendation by another organization but it was not (so far, after 24 years) made part of the standard.
  • reading it closely, you may notice that the standard is vague in many places, allowing different interpretations. It describes the syntax of these control sequences but does not describe their behavior. For that, you have to go to the documentation for the respective terminals.
  • documentation for xterm is found in XTerm Control Sequences, which assumes a familiarity with DEC documentation found on http://vt100.net
  • for other terminals, their manual pages may enumerate the controls (as done for Linux console in console_codes).
  • other terminals document themselves as "xterm"; for discussion see the xterm FAQ Comparing versions, by counting controls. Fortunately, the color controls are mostly compatible (but see the ncurses FAQ My terminal shows some uncolored spaces).
  • you can also read the manual pages for tput, which allows you to use terminfo capabilities in a shell script. Your example could be rewritten as

    printf '%s\n' "$(tput setaf 4)Hello$(tput setaf 0)"
    
  • a few sources such as the Bash Prompt HOWTO and the Advanced Bash-Scripting Guide mention color, but (even these) as a rule have gaps and omissions and are not helpful with respect to ECMA-48.

  • most of the other sources which you will find with a web-search copy from other sources (such as those cited), etc., and as a rule pay little attention to the actual standard.