Creating a configuration file that contains common variables and settings can go a long way in keeping the code neat and organized. This allows us to use these configuration settings in different scripts while managing them from one central file.

In this example, I’ll create a new text.sh file under ~/scripts/config/text.sh. It will hold all of the text formatting variables.

text.sh

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"

I can then load this text.sh file into other Bash scripts instead of copying and pasting its contents into each one.

script.sh

#!/bin/sh

# Load text formatting.
. ~/scripts/config/text.sh

echo -e "${GREEN}This is a green text.${CLEAR}"

or by using the source command:

#!/bin/sh

# Load text formatting.
source ~/scripts/config/text.sh

echo -e "${GREEN}This is a green text.${CLEAR}"