Bash - Print numbers with color: Negative is red and positive is green

By xngo on February 28, 2019

#!/bin/bash
set -e
# Description: Print numbers with color: Negative is red and positive is green.
 
function F_PRINT_COLOR_NUMBERS()
{
  local LABEL=$1
  local VALUE=$2
  local COLOR_CODE=""
  if [ "${VALUE}" -lt 0 ]; then
    COLOR_CODE=$(tput setaf 1) # red color.
  else
    COLOR_CODE=$(tput setaf 2) # green color.
  fi
 
  local RESET_COLOR=$(tput sgr0)
  printf "%22s: ${COLOR_CODE}%'6d${RESET_COLOR}\n" "${LABEL}" "${VALUE}"
}
 
F_PRINT_COLOR_NUMBERS "Oil"      7921
F_PRINT_COLOR_NUMBERS "Gasoline" -369

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.