Skip to main content

Enhancing Shell Scripts with Colored Terminal Output

·335 words·2 mins
Linux Shell Bash Terminal Scripting
Table of Contents

Enhancing Shell Scripts with Colored Terminal Output
#

Adding color to shell scripts is not just cosmetic—it significantly improves readability, usability, and user feedback. By visually distinguishing errors, warnings, and success messages, scripts become easier to understand and safer to operate, especially during automation and troubleshooting.


🎨 How ANSI Escape Codes Work
#

Terminal colors and text styles are controlled using ANSI escape codes. These are special character sequences interpreted by the terminal to modify how text is displayed.

The basic format is:


\033[<STYLE>;<COLOR>m<TEXT>\033[0m

Key components:

  • \033[ or \e[: Starts the escape sequence
  • <STYLE>;<COLOR>m: Defines formatting and color
  • \033[0m: Resets formatting — always include this to avoid color bleed

🛠️ ANSI Color Code Reference
#

Foreground (Text) Colors
#

Color Code Color Code
Black \033[30m Blue \033[34m
Red \033[31m Purple \033[35m
Green \033[32m Cyan \033[36m
Yellow \033[33m White \033[37m

Background Colors
#

Color Code Color Code
Black \033[40m Blue \033[44m
Red \033[41m Purple \033[45m
Green \033[42m Cyan \033[46m
Yellow \033[43m White \033[47m

💻 Professional Scripting Pattern
#

To keep scripts clean and maintainable, define color codes as variables near the top of the script instead of repeating escape sequences.

#!/bin/bash

# Color definitions
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
NC='\033[0m'   # Reset / No Color

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Example output
log_success "Backup completed successfully."
log_warning "Disk space is reaching 85%."
log_error "Database connection failed."

This approach improves consistency and makes future color changes trivial.


✨ Advanced Text Formatting
#

ANSI codes can be combined using semicolons to apply multiple styles at once:

  • Bold red: \033[1;31m
  • Underlined green: \033[4;32m
  • Inverted colors: \033[7m

Compatibility Notes
#

  • Terminal emulators (iTerm2, GNOME Terminal, VS Code, PuTTY) may render colors differently
  • Always use echo -e or printf to ensure escape sequences are interpreted correctly
  • For non-interactive scripts, consider disabling colors when output is redirected

Using colored output thoughtfully makes shell scripts clearer, safer, and more professional—especially in automation, monitoring, and system administration workflows.

Related

Mastering AWK for Powerful Text Processing on Linux
·446 words·3 mins
Linux Awk Shell Text-Processing Cli
SSH Passwordless Authentication on Linux: A Complete Guide
·466 words·3 mins
Linux SSH Security System Administration
Ubuntu 26.04 LTS Arrives April 23 with GNOME 50 and Rust Core
·543 words·3 mins
Ubuntu Linux LTS GNOME Rust