23

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?

  • 2
    You're looking for documentation on ANSI escape codes. (I tend to use the Wikipedia page when I need to look one up.) – chepner Nov 7 '15 at 21:34
  • Note that you don't necessarily need to know those escape sequences. Several shells (tcsh, zsh, fish at least) have builtin support to output in colours without having to explicitely write those sequences. – Stéphane Chazelas Nov 7 '15 at 22:06
27

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.
  • The relevant section in ECMA-48 seems to be 8.3.117 - "SGR - SELECT GRAPHIC RENDITION". It has the only mentions of the word "blue", and the codes found there work at least in my XTerm. – l0b0 Nov 7 '15 at 11:42
  • yes - I'll add a clarification. – Thomas Dickey Nov 7 '15 at 11:43
  • 2
    It's really great answer - wish all answers be like yours – Sergius Nov 7 '15 at 16:48
  • 1
    xterm also support 256-color mode, presumably this is documented in one of the manuals above. – sendmoreinfo Nov 7 '15 at 18:48
  • See wynnnetherland.com/journal/… for a bunch of useful info including the grc package which can do all sorts of colorizing things with command output. – Joe Nov 13 '15 at 23:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.