Skip to content

Set terminal colour variables

Populate tput colour variables when stdout is a colour-capable terminal.

18th August 2026

set_colours() {
  fgRed=''
  fgYellow=''
  bgBlack=''
  bgWhite=''
  reset=''

  if test -t 1; then
    ncolors=$(tput colors)

    if test -n "${ncolors}" && test "${ncolors}" -ge 8; then
      fgRed=$(tput setaf 1)
      fgYellow=$(tput setaf 3)
      bgBlack=$(tput setab 0)
      bgWhite=$(tput setab 7)
      reset=$(tput sgr0)
    fi
  fi
}