Differences between revisions 1 and 9 (spanning 8 versions)
Revision 1 as of 2007-05-02 23:26:33
Size: 775
Editor: redondos
Comment:
Revision 9 as of 2010-10-08 06:29:50
Size: 2634
Editor: 87
Comment: Contentious edit, but it's also true.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq37)]] <<Anchor(faq37)>>
Line 3: Line 3:
''Do not'' hard-code ANSI color escape sequences in your program! The {{{tput}}} command lets you interact with the terminal database in a sane way. ''Do not'' hard-code ANSI color escape sequences in your program! The {{{tput}}} command lets you interact with the terminal database in a sane way:
Line 6: Line 6:
  # Bourne
Line 8: Line 9:
  tput setaf 0; echo now we are back in black   tput bold; echo "boldface (and still green)"
  tput sgr0; echo back to normal
Line 10: Line 12:

  This will be contentious, but I'm going to disagree and recommend you use hard-coded ANSI escape sequences because terminfo databases in the real world are too often broken.

  tput setaf literally means "Set ANSI foreground" and shouldn't have any difference with a hard-coded ANSI escape sequence, except that it will actually work with broken terminfo databases so your colors will look correct in a VT with terminal type linux-16color or any terminal type so long as it really is a linux terminal capable of 16 ANSI colors.

  So ''do'' consider setting those variables to hard-coded ANSI sequences such as:

  {{{
    # Bash
    white="<Ctrl+v><Esc>[0;37m"
  }}}

  If you want it to work properly in the real world. Note you press <Ctrl+v> followed by a literal escape key to insert the escape key as a literal byte value into the variable in most editors, and the resulting output looks like:

  {{{
    # Bash
    white="^[[0;370m"
  }}}
Line 13: Line 33:
If you don't know in advance what your user's terminal's default text color is, you can use {{{tput sgr0}}} to reset the colors to their default settings. This also removes boldface ({{{tput bold}}}), etc. {{{tput sgr0}}} resets the colors to their default settings. This also turns off boldface ({{{tput bold}}}), underline, etc.

If you want fancy colors in your prompt, consider using something manageable:

{{{
  # Bash
  red=$(tput setaf 1)
  green=$(tput setaf 2)
  blue=$(tput setaf 4)
  reset=$(tput sgr0)
  PS1='\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\]\w\[$reset\]\$ '
}}}

Note that we do '''not''' hard-code ANSI color escape sequences. Instead, we [[BashFAQ/002|store the output]] of the `tput` command into variables, which are then used when `$PS1` is expanded. Storing the values means we don't have to fork a `tput` process multiple times every time the prompt is displayed; `tput` is only invoked 4 times during [[DotFiles|shell startup]]. The `\[` and `\]` symbols allow bash to understand which parts of the prompt cause no cursor movement; without them, [[BashFAQ/053|lines will wrap incorrectly]].

See also [[http://wiki.bash-hackers.org/scripting/terminalcodes]] for an overview.

----
CategoryShell

How can I print text in various colors?

Do not hard-code ANSI color escape sequences in your program! The tput command lets you interact with the terminal database in a sane way:

  # Bourne
  tput setaf 1; echo this is red
  tput setaf 2; echo this is green
  tput bold; echo "boldface (and still green)"
  tput sgr0; echo back to normal
  • This will be contentious, but I'm going to disagree and recommend you use hard-coded ANSI escape sequences because terminfo databases in the real world are too often broken. tput setaf literally means "Set ANSI foreground" and shouldn't have any difference with a hard-coded ANSI escape sequence, except that it will actually work with broken terminfo databases so your colors will look correct in a VT with terminal type linux-16color or any terminal type so long as it really is a linux terminal capable of 16 ANSI colors.

    So do consider setting those variables to hard-coded ANSI sequences such as:

        # Bash
        white="<Ctrl+v><Esc>[0;37m"

    If you want it to work properly in the real world. Note you press <Ctrl+v> followed by a literal escape key to insert the escape key as a literal byte value into the variable in most editors, and the resulting output looks like:

        # Bash
        white="^[[0;370m"

tput reads the terminfo database which contains all the escape codes necessary for interacting with your terminal, as defined by the $TERM variable. For more details, see the terminfo(5) man page.

tput sgr0 resets the colors to their default settings. This also turns off boldface (tput bold), underline, etc.

If you want fancy colors in your prompt, consider using something manageable:

  # Bash
  red=$(tput setaf 1)
  green=$(tput setaf 2)
  blue=$(tput setaf 4)
  reset=$(tput sgr0)
  PS1='\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\]\w\[$reset\]\$ '

Note that we do not hard-code ANSI color escape sequences. Instead, we store the output of the tput command into variables, which are then used when $PS1 is expanded. Storing the values means we don't have to fork a tput process multiple times every time the prompt is displayed; tput is only invoked 4 times during shell startup. The \[ and \] symbols allow bash to understand which parts of the prompt cause no cursor movement; without them, lines will wrap incorrectly.

See also http://wiki.bash-hackers.org/scripting/terminalcodes for an overview.


CategoryShell

BashFAQ/037 (last edited 2023-10-01 21:59:10 by larryv)