Mastering Colored Output in Shell Scripts with echo and printf
This guide explains how to use echo and printf with ANSI escape sequences to produce colored and styled terminal output in Bash, covering syntax, color codes, attribute combinations, cursor control, predefined variables, and practical examples.
Both echo and printf can output content in shell scripts, and with the -e option they can display colored text using ANSI escape sequences.
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 resulting output shows the specified background and foreground colors.
Format Explanation
In Bash, echo displays content, and to show colors you must use the -e option. The general format is: echo -e "\033[background;colormString\033[0m" For example: echo -e "\033[41;36m something here \033[0m" Here 41 sets the background color (red) and 36 sets the foreground color (cyan).
Notes
Background and foreground colors are separated by a semicolon ";".
The color code ends with the letter m.
Spaces before or after the string are preserved in the output.
Multiple attributes can be combined using semicolons, e.g., \033[5;4;47;30m for blink, underline, white background, black text.
Combined Commands Example
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 prints the text in bold, underlined, green, and finally resets attributes.
Attribute Control Codes
\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)Example color commands:
echo -e "\033[31m Red text \033[0m"
echo -e "\033[34m Yellow text \033[0m"
echo -e "\033[41;33m Red background, yellow text \033[0m"
echo -e "\033[41;37m Red background, white text \033[0m"Foreground Color Range (30‑39)
echo -e "\033[30m Black \033[0m"
echo -e "\033[31m Red \033[0m"
echo -e "\033[32m Green \033[0m"
echo -e "\033[33m Yellow \033[0m"
echo -e "\033[34m Blue \033[0m"
echo -e "\033[35m Purple \033[0m"
echo -e "\033[36m Cyan \033[0m"
echo -e "\033[37m White \033[0m"
# 38: underline on default color
# 39: underline offBackground Color Range (40‑47)
40: Black
41: Dark Red
42: Green
43: Yellow
44: Blue
45: Purple
46: Dark Green
47: White
49: Default backgroundCursor Position and Screen Control
\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 to row y, column x
\33[2J Clear screen
\33[K Erase to end of line
\33[s Save cursor position
\33[u Restore cursor position
\33[?25l Hide cursor
\33[?25h Show cursorPredefined Color Variables
When running a Bash script repeatedly, you can define variables for color codes to simplify usage:
#! /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 blue color===${RESET}"Countdown Example Using Carriage Return
for i in {1..100}; do
echo -en "\r $i%"
sleep 1
doneSave the script as myDisplay.sh, make it executable, and run it on a Linux server to see the effects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
