0
0
Bash Scriptingscripting~5 mins

Color output (ANSI escape codes) in Bash Scripting

Choose your learning style9 modes available
Introduction
Using colors in terminal output makes messages easier to see and understand quickly.
Highlight errors or warnings in scripts
Make success messages stand out
Differentiate types of information in logs
Improve readability of command line tools
Add visual appeal to script outputs
Syntax
Bash Scripting
echo -e "\e[COLOR_CODEmYour text here\e[0m"
Use \e or \033 to start an ANSI escape code.
End the color with \e[0m to reset back to normal text.
Examples
Prints text in red color.
Bash Scripting
echo -e "\e[31mThis is red text\e[0m"
Prints text in green color.
Bash Scripting
echo -e "\e[32mThis is green text\e[0m"
Prints bold blue text using two codes separated by a semicolon.
Bash Scripting
echo -e "\e[1;34mThis is bold blue text\e[0m"
Sample Program
This script prints four messages, each in a different color to show how to use ANSI escape codes for color output.
Bash Scripting
#!/bin/bash

# Print messages in different colors

echo -e "\e[31mError: Something went wrong!\e[0m"
echo -e "\e[32mSuccess: Operation completed!\e[0m"
echo -e "\e[33mWarning: Check your input.\e[0m"
echo -e "\e[1;34mInfo: Process started.\e[0m"
OutputSuccess
Important Notes
Not all terminals support colors, but most modern ones do.
Use \e[0m after colored text to avoid coloring everything after.
You can combine codes like bold (1) and color (e.g., 34 for blue) with a semicolon.
Summary
ANSI escape codes add color to terminal text.
Start with \e[ and end with m, reset with \e[0m.
Colors help make script output clearer and easier to read.