Operations 11 min read

Master Colorful Terminal Output with echo and printf in Bash

This guide explains how to use echo and printf with ANSI escape codes in Bash scripts to produce colored text, background colors, text effects, cursor positioning, and reusable variables, providing numerous examples and visual demonstrations for terminal output customization.

Raymond Ops
Raymond Ops
Raymond Ops
Master Colorful Terminal Output with echo and printf in Bash

Both echo and printf can output content in shell scripts. Using the -e option enables interpretation of escape sequences such as ANSI color codes.

echo -e "\033[43;35m david use echo say Hello World \033[0m 
"
printf "\033[44;36m david use printf say Hello World \033[0m 
"
echo -e "\033[47;30;5m david use echo say \033[0m Hello World 
"

The output appears with the specified colors and effects.

Image
Image

Format Explanation

In shell scripts, the echo command displays content; to show colored text you need the -e parameter. The format is:

echo -e "\033[BackgroundColor;TextColormString\033[0m"

Example: echo -e "\033[41;36m something here \033[0m" Here 41 sets the background color and 36 sets the text color.

Combination Commands

Example of combining commands to move the cursor and apply multiple text attributes:

echo -e "\033[20;1H\033[1;4;32m david use echo say \033[0m Hello World 
"

This moves the cursor to line 20, column 1, then sets high‑light, underline, and green color before printing the text, finally resetting attributes with \033[0m.

Image
Image

Effect Control

Common ANSI codes for text effects:

\033[0m   Reset all attributes
\033[1m   Bold
\033[4m   Underline
\033[5m   Blink
\033[7m   Reverse video
\033[8m   Conceal
\033[30m‑\033[37m   Set foreground color (30‑37)
\033[40m‑\033[47m   Set background color (40‑47)

Examples:

echo -e "\033[31m red \033[0m"
echo -e "\033[34m blue \033[0m"
echo -e "\033[41;33m red on yellow \033[0m"
echo -e "\033[41;37m red on white \033[0m"

Cursor and Screen Control

ANSI sequences for cursor movement and screen manipulation:

\33[nA   Move cursor up n lines
\33[nB   Move cursor down n lines
\33[nC   Move cursor right n columns
\33[nD   Move cursor left n columns
\33[y;xH Set cursor position
\33[2J   Clear screen
\33[K    Erase from cursor to end of line
\33[s    Save cursor position
\33[u    Restore cursor position
\33[?25l Hide cursor
\33[?25h Show cursor

Predefined Color Variables

Define color codes as variables for reuse in Bash scripts:

#! /bin/bash
RED_COLOR='\E[1;31m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RESET='\E[0m'

echo -e "${RED_COLOR}===david say red color===${RESET}"
echo -e "${YELLOW_COLOR}===david say yellow color===${RESET}"
echo -e "${BLUE_COLOR}===david say green color===${RESET}"
Image
Image

Define actions as variables to simplify color output and other effects, then invoke them directly.

# Define actions
SETCOLOR_BLACK="echo -en \\E[1;30m"
SETCOLOR_FAILURE="echo -en \\E[1;31m"
SETCOLOR_SUCCESS="echo -en \\E[1;32m"
SETCOLOR_WARNING="echo -en \\E[1;33m"
SETCOLOR_INFO="echo -en \\E[1;34m"
SETCOLOR_BUG="echo -en \\E[1;36m"
SETCOLOR_NORMAL="echo -en \\E[0;39m"
SETCOLOR_FLICKER="echo -en \\E[5m"

# Use actions
$SETCOLOR_BLACK && echo BLACK
$SETCOLOR_FAILURE && echo FAILURE
$SETCOLOR_SUCCESS && echo SUCCESS
$SETCOLOR_WARNING && echo WARNING
$SETCOLOR_INFO && echo INFO
$SETCOLOR_BUG && echo BUG
$SETCOLOR_NORMAL && echo NORMAL
$SETCOLOR_FLICKER && echo FLICKER
$SETCOLOR_NORMAL && echo NORMAL

Countdown Example Using Carriage Return

Show a progress countdown by returning to the line start:

for i in {1..100}; do
  echo -en "\r $i%"
  sleep 1
done

Save the script as myDisplay.sh, make it executable, and run it on a Linux server to see the effect.

Image
Image
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BashShell scriptingterminalcolorprintfEcho
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.