Add Colour to Text on the Terminal: Examples with Bash and Python

Add Colour to Text on the Terminal: Examples with Bash and Python

Last updated:
Table of Contents

These messages work within any text printed to a Terminal.

They can therefore be used in other scripts you create (php, ruby, perl, etc), not just bash or Python.

See full color codes here

Summary

ColorCodeOutput
GREEN\033[32mTEXT\033[00m
YELLOW\033[33mTEXT\033[00m
RED\033[31mTEXT\033[00m
BLUE\033[34mTEXT\033[00m
BOLD GREEN\033[1;32mTEXT\033[00m
BOLD YELLOW\033[1;33mTEXT\033[00m
BOLD RED\033[1;31mTEXT\033[00m
BOLD BLUE\033[1;31mTEXT\033[00m

Red text

Wrap your string with "\033[31m" and "\033[00m":

Example: Print "ERROR" in red

#!/usr/bin/env bash

# must use double quotes
red_prefix="\033[31m"
red_suffix="\033[00m"

# wrap the string "ERROR" with the prefix and suffix
echo -e "$red_prefix"ERROR"$red_suffix"

red-text-bash-terminal ERROR printed in red colour

Yellow text

Wrap your string with "\033[33m" and "\033[00m":

Example: Print "WARNING" in yellow

#!/usr/bin/env bash

# must use double quotes
yellow_prefix="\033[33m"
yellow_suffix="\033[00m"

# wrap the string "WARNING" with the prefix and suffix
echo -e "$yellow_prefix"WARNING"$yellow_suffix"

bash-terminal-yellow-text WARNING printed in yellow colour

Bold Yellow text

Wrap your string with "\033[1;33m" and "\033[00m":

Example: Print "WARNING" in bold yellow

#!/usr/bin/env bash

# must use double quotes
bold_yellow_prefix="\033[1;33m"
bold_yellow_suffix="\033[00m"

# wrap the string "WARNING" with the prefix and suffix
echo -e "$bold_yellow_prefix"WARNING"$bold_yellow_suffix"

bash-terminal-bold-yellow-text WARNING printed in bold yellow colour

Bold Green text

Wrap your text with "\033[1;32m" and "\033[00m"

Example: Print "SUCCESS" in bold green

#!/usr/bin/env bash

# must use double quotes
bold_green_prefix="\033[1;32m"
bold_green_suffix="\033[00m"

# wrap the string "SUCCESS" with the prefix and suffix
echo -e "$bold_green_prefix"SUCCESS"$bold_green_suffix"

bold-green-text-terminal-output SUCCESS printed in bold green colour