Differences between revisions 1 and 2
Revision 1 as of 2007-05-02 23:26:33
Size: 775
Editor: redondos
Revision 2 as of 2007-06-18 21:57:51
Size: 871
Editor: p54934E54
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:

See also [http://bash-hackers.org/wiki/doku.php?id=scripting:terminalcodes] for an overview.


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.

  tput setaf 1; echo this is red
  tput setaf 2; echo this is green
  tput setaf 0; echo now we are back in black

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.

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.

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)