Bash

ANSI Color Output Helpers

admin by @admin ADMIN
19m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Wrap printf with ANSI escape codes for color and bold. Auto-disable if stdout isn't a TTY (so piping to a file doesn't pollute it with escape sequences).
Bash
Raw
# Detect if we can use color
if [[ -t 1 ]]; then
    RED=$'\e[31m'; GRN=$'\e[32m'; YEL=$'\e[33m'; BLU=$'\e[34m'
    BOLD=$'\e[1m'; DIM=$'\e[2m'; RST=$'\e[0m'
else
    RED=""; GRN=""; YEL=""; BLU=""; BOLD=""; DIM=""; RST=""
fi

ok()   { printf "${GRN}✓${RST} %s\n" "$*"; }
warn() { printf "${YEL}⚠${RST} %s\n" "$*"; }
err()  { printf "${RED}✗${RST} %s\n" "$*" >&2; }
info() { printf "${BLU}ℹ${RST} %s\n" "$*"; }
hdr()  { printf "${BOLD}%s${RST}\n" "$*"; }

ok   "Build completed in 12s"
warn "Deprecation: rename module before next release"
err  "Database connection refused"
info "Loaded config from /etc/myapp.toml"
hdr  "== Deployment Summary =="
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.