It’s easier to detect an error if it’s marked in red.

It’s easier to detect a successfully completed action if it’s in green.

Colors, bold and underline - can make the text in a Bash script much easier to read.

Fortunately, it’s easy to apply some text formatting to Bash scripts.

Text formatting variables

I created this list of variables once:

### Text formatting ###

CLEAR="\e[0m"

# Text settings.
BOLD="\e[1m"
UNDERLINE="\e[4m"

# Text color.
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"

# Text color with bold font.
RED_BOLD="\e[1;31m"
GREEN_BOLD="\e[1;32m"
YELLOW_BOLD="\e[1;33m"
BLUE_BOLD="\e[1;34m"
MAGENTA_BOLD="\e[1;35m"
CYAN_BOLD="\e[1;36m"

# Background color.
RED_BG="\e[41m"
GREEN_BG="\e[42m"
YELLOW_BG="\e[43m"
BLUE_BG="\e[44m"
MAGENTA_BG="\e[45m"
CYAN_BG="\e[46m"

# Background color with bold font.
RED_BG_BOLD="\e[1;41m"
GREEN_BG_BOLD="\e[1;42m"
YELLOW_BG_BOLD="\e[1;43m"
BLUE_BG_BOLD="\e[1;44m"
MAGENTA_BG_BOLD="\e[1;45m"
CYAN_BG_BOLD="\e[1;46m"

### End of text formatting ###

Then, I just copy and paste this list to the top of my new Bash script (right after #! /bin/bash).

I can now use any of the formatting settings I want.

Usage

Once we include this list of variables at the top of the script, we can simply call the relevant variables as part of the echo -e command.

For example:

echo -e "${BOLD}This text is bold.${CLEAR} This text is normal."

The text between ${BOLD} and ${CLEAR} is going to be bold.

The text after ${CLEAR} will appear in the standard (default) format.

Another example:

echo -e "${GREEN_BG_BOLD}This text is bold and has a green background.${CLEAR} This text is normal."

The text between ${GREEN_BG_BOLD} and ${CLEAR} will be bold with a green background.

Bold font in Git Bash

I use Git Bash as my terminal.

I noticed that at first, the “bold” variables did not make the text more bold, but only changed its color to be lighter.

I found the solution in the terminal settings:

  1. Right-click the Git Bash window » “Options…”.
  2. Click the “Text” tab in the left menu.
  3. Change the selection under “Show bold”. Select as font & as colour to make the color lighter but also the font bolder. I use the “xterm” as the theme (“Looks” tab), so I chose the xterm option here as well.