Linux Basic — ANSI Colors

less than 1 minute read

ANSI Colors

Colors make menus readable, but only when used correctly. Detect a terminal, respect NO_COLOR, and provide a fallback.


Safe Color Helper

# call once at startup
if [[ -t 1 && -z "$NO_COLOR" ]]; then
  RED="\e[31m"; GREEN="\e[32m"; YELLOW="\e[33m"; RESET="\e[0m"
else
  RED=""; GREEN=""; YELLOW=""; RESET=""
fi

For more portability use tput setaf 1 etc., but this requires terminfo.


--no-color Support

Honor either NO_COLOR=1 env var or a --no-color flag to disable color output for logging and piping.


Next → Exit & Quit Handling

Updated: