Differences between revisions 3 and 4
Revision 3 as of 2008-02-19 20:54:35
Size: 1638
Editor: GreyCat
Comment: more (and improved) examples, link to a couple other FAQs
Revision 4 as of 2008-05-16 19:38:49
Size: 1647
Editor: GreyCat
Comment: adjust links
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
  # Bourne
Line 16: Line 17:
If you want fancy colors in your prompt (in bash), consider using something manageable: If you want fancy colors in your prompt, consider using something manageable:
Line 19: Line 20:
  # Bash
Line 26: Line 28:
Note that we do '''not''' hard-code ANSI color escape sequences. Instead, we [:BashFAQ#faq2: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#faq53:lines will wrap incorrectly]. 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].

Anchor(faq37)

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

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)
  NORM=$(tput sgr0)
  PS1='\[$RED\]\u\[$NORM\]@\[$GREEN\]\h\[$NORM\]:\[$BLUE\]\w\[$NORM\]\$ '

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://bash-hackers.org/wiki/doku.php?id=scripting:terminalcodes] for an overview.

BashFAQ/037 (last edited 2022-08-01 09:45:25 by 84)