This question already has an answer here:

I want to write a bash script that prints some information to the terminal STDOUT. For better readability and structure, I would like to color the different items and highlight them this way.

Now, is there a Bash equivalent to the Windows-CMD/DOS command COLOR which sets the currently used background and font color? And how do I reset the color back to the terminal default after finishing my script?

I have read Pipable command to print in color?, but only some answers are written in Bash so that they can be easily included in my script. Besides this, the answers there lack the following points I want:

  • change background color
  • display white foreground color, the default color set in gnome-terminal gets displayed instead (which is neon green for me)
  • make all available colors accessible somehow, not only a small subset by keywords. I could imagine e.g. some kind of RGB encoding to pass as function argument for easily specifying a color.

marked as duplicate by muru, Eric Carvalho, 2707974, v2r, waltinator Sep 25 '15 at 18:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @dschinn1001 I don't understand what gedit has to do with that. It's a GUI editor, not for the terminal. – Byte Commander Sep 22 '15 at 18:58
  • @ByteCommander - sorry, now I know what you mean - have misunderstood you but in google there is much output with terms "howto write bash-script with colour output" – dschinn1001 Sep 22 '15 at 19:03
  • I have read some that insert some very cryptic strings into the text to output, for changing the color, but I hoped there has to be something easier... – Byte Commander Sep 22 '15 at 19:09
  • @muru The question you linked is only a subset of mine. I want more! ;-) – Byte Commander Sep 22 '15 at 19:17
  • @ByteCommander most terminals in use support 256 colours, not the full RGB set. You want 256 names in your code? Are you insane? – muru Sep 22 '15 at 19:18

A small script to get the cryptic codes ;)

#   This file echoes a bunch of color codes to the 
#   terminal to demonstrate what's available.  Each 
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a 
#   test use of that color on all nine background 
#   colors (default + 8 escapes).

T='gYw'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";

enter image description here

The code is easy to read from the table that generated the previous script.


  • \e[1;37m – Foreground

  • \e[44m – Background

Some examples

  • White foreground

    TOA="\e[0m" # No Color
    echo -e "${whiteForeground}foo${TOA}"
  • White on blue

    TOA="\e[0m" # No Color
    echo -e "${whiteOnBlue}foo${TOA}"

And a sample bash script:

TOA="\e[0m" # No Color

if [[ $cfg_log_level == "" ]]; then

function logError () {
  if [[ $cfg_log_level == "1" ]] || [[ $cfg_log_level == "2" ]] || [[ $cfg_log_level == "3" ]] || [[ $cfg_log_level == "4" ]]; then
    echo -e "${red}$1${TOA}" 1>&2

function logWarn () {
  if [[ $cfg_log_level == "2" ]] || [[ $cfg_log_level == "3" ]] || [[ $cfg_log_level == "4" ]]; then
    echo -e "${yellow}$1${TOA}" 1>&2

function logInfo () {
  if [[ $cfg_log_level == "3" ]] || [[ $cfg_log_level == "4" ]]; then
    echo -e "${green}$1${TOA}" 1>&2

function logDebug () {
  if [[ $cfg_log_level == "4" ]]; then
    echo -e "${blue}$1${TOA}" 1>&2
  • Wow! Saved to ` /usr/local/bin` – Fabby Sep 23 '15 at 18:56

You could use tput

  • tput setaf <color> sets the foreground color
  • tput setab <color> sets the background color

the colors are 1 bit b,g,r:

  0. black
  1. red
  2. green
  3. yellow
  4. blue
  5. magenta
  6. cyan
  7. white

tput can set other attributes of the characters such as underline, bold, blink etc. see man terminfo for more information.

I think that a good way of using this is by setting variables shemantically. i.e.

startheader=`tput setaf 0``tput setab 7`
endheader=`tput sgr0`
startprompt=`tput bold`

echo ${startheader}The Header${endheader}
echo -n ${startprompt}Enter value: ${endprompt}
read value

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