How to Use Colors in Bash Scripts Made Simple

Color can be added to a terminal window output in Bash scripts using escape codes. Note that the effects will differ depending on the client program being used to connect. The examples below were executed via a Putty ssh terminal.

Bash Color Codes

  • There are foreground colors, background colors, and text styles
  • Not all are supported based on the client terminal program!
  • Color codes are based on special “escape sequences” (such as “\e[32m” for Green)
  • Use “echo -e” to have Bash properly interpret the escape code:

$ echo -e "\e[31mRed Text!"

  • Finally, if you wish to turn off a color or style, use the appropriate reset code:

$ echo -e "\e[93m\e[46mYellow on Cyan\e[0m Normal"

  •  Note that the background wasn’t explicitly turned off… the “0” value resets everything

A Simple Color Script to be Included in Bash Scripts

The following script provides color constants that can be quickly used in your Bash scripts.

  • Save the script below as “colors.sh
  • Place the script somewhere on your path (when I wrote this, it was originally for admin scripts and I placed it in /usr/sbin)
  • Include it in your bash script by adding this line:

source colors.sh

  • Be sure to change permissions to allow execution:

$ chmod 755 colors.sh

  • The script has a test function labelled “func_cdisplay” which outputs foreground/background combinations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash

# colors.sh
# Version KAS 150412
# This script defines a set of constants for foreground colors,
# background colors, and console text styles to be used in bash
# console scripts.
#
# To use this script:
#
# In your bash script, near the top, add:
# source colors.sh
#
# Use echo -e to handle escape codes, and use ${constant} to
# insert a color code:
# echo -e ${cdc}This is dark cyan.${coff}
#
# Use the Color Off constant "coff", the Background Color Off
# constant "boff" and the Style Off constant "soff" to clear
# formatting:
# echo -e ${cly}${bdb}Classic ${sun}Yellow${sunf} on Blue${coff}
#
# Codes are abbreviated for quick insertion.  Abbreviation format:
# 1st:   c: color         b: background color           s: style
# 2nd:   l: light         d: dark
# rest:  color / style

# Foreground (Text) Colors
cbl="\e[30m"       # Color Black
cdr="\e[31m"       # Color Dark Red
cdg="\e[32m"       # Color Dark Green
cdy="\e[33m"       # Color Dark Yellow
cdb="\e[34m"       # Color Dark Blue
cdm="\e[35m"       # Color Dark Magenta
cdc="\e[36m"       # Color Dark Cyan
clgy="\e[37m"      # Color Light Gray

cdgy="\e[90m"      # Color Dark Gray
clr="\e[91m"       # Color Light Red
clg="\e[92m"       # Color Light Green
cly="\e[93m"       # Color Light Yellow
clb="\e[94m"       # Color Light Blue
clm="\e[95m"       # Color Light Magenta
clc="\e[96m"       # Color Light Cyan
cwh="\e[97m"       # Color White

# Turns off all formatting
coff="\e[0m"       # Color Off

# Background Colors
bbl="\e[40m"       # Background Color Black
bdr="\e[41m"       # Background Color Dark Red
bdg="\e[42m"       # Background Color Dark Green
bdy="\e[43m"       # Background Color Dark Yellow
bdb="\e[44m"       # Background Color Dark Blue
bdm="\e[45m"       # Background Color Dark Magenta
bdc="\e[46m"       # Background Color Dark Cyan
blgy="\e[47m"      # Background Color Light Gray

bdgy="\e[100m"     # Background Color Dark Gray
blr="\e[101m"      # Background Color Light Red
blg="\e[102m"      # Background Color Light Green
bly="\e[103m"      # Background Color Light Yellow
blb="\e[104m"      # Background Color Light Blue
blm="\e[105m"      # Background Color Light Magenta
blc="\e[106m"      # Background Color Light Cyan
bwh="\e[107m"      # Background Color White

# Turns off only the background color
boff="\e[49m"      # Background Color Off

# Styles
sbo="\e[1m"        # Style Bold
sdi="\e[2m"        # Style Dim
sun="\e[4m"        # Style Underline
sbl="\e[5m"        # Style Blink
sre="\e[7m"        # Style Reverse
shi="\e[8m"        # Style Hidden

sbof="\e[21m"      # Style Bold Off
sdif="\e[22m"      # Style Dim Off
sunf="\e[24m"      # Style Underline Off
sblf="\e[25m"      # Style Blink Off
sref="\e[27m"      # Style Reverse Off
shif="\e[28m"      # Style Hidden Off


# A test function which illustrates combinations
func_cdisplay ()
{
   local text="MXW"
   local formatted=""
   local colors=( $cbl $cdr $cdg $cdy $cdb $cdm $cdc $clgy \      
                  $cdgy $clr $clg $cly $clb $clm $clc $cwh )
   local bgs=( $bbl $bdr $bdg $bdy $bdb $bdm $bdc $blgy \      
               $bdgy $blr $blg $bly $blb $blm $blc $bwh )

   for color in ${colors[@]}
   do
      for bg in ${bgs[@]}
      do
         formatted=" $formatted ${bg}${color}${text}${coff}"
      done

      echo -e $formatted
      formatted=""
   done

   echo
   echo -e "${sbo}Bold${sbof} ${sdi}Dim${sdif} " \
           "${sun}Underline${sunf} ${sbl}Blink${sblf} " \
           "${sre}Reverse${sref} .${shi}Hidden${shif}."
   echo
}

The following is example output of the “func_cdisplay” function on a Putty ssh session window:

bashcolor1

Some styles aren’t supported, such as Blink or Hidden.   It is a good idea to call “func_cdisplay” under your client to see what is supported and what the best combinations are.

That’s it.

k

 

Karim Sultan
My vision is for digital innovation across the divide, by securing and simplifying the Internet and extending its reach to every citizen on Earth and beyond.

Leave a Reply

Your email address will not be published. Required fields are marked *