Skip to content

Instantly share code, notes, and snippets.

@demonkoryu
Created February 24, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demonkoryu/08fab4f20667a7ee60d18de54bf29531 to your computer and use it in GitHub Desktop.
Save demonkoryu/08fab4f20667a7ee60d18de54bf29531 to your computer and use it in GitHub Desktop.
Bash colored echo
#!/bin/bash
# if terminal, use colors
if [ -t 1 ]; then
function tp {
tput "$@"
}
# else, no colored output to prevent messing up log files
else
function tp {
echo ""
}
fi
function black { echo "$(tp setaf 0)$@$(tp sgr0)"; }
function red { echo "$(tp setaf 1)$@$(tp sgr0)"; }
function green { echo "$(tp setaf 2)$@$(tp sgr0)"; }
function yellow { echo "$(tp setaf 3)$@$(tp sgr0)"; }
function blue { echo "$(tp setaf 4)$@$(tp sgr0)"; }
function magenta { echo "$(tp setaf 5)$@$(tp sgr0)"; }
function cyan { echo "$(tp setaf 6)$@$(tp sgr0)"; }
function white { echo "$(tp setaf 7)$@$(tp sgr0)"; }
function nocolor { echo "$(tp sgr0 0) $@$(tp sgr0)"; }
function check { green "$@"; }
function success { yellow "$@"; }
function error { red "$@"; }
function info { white "$@"; }
function text { nocolor "$@"; }
# example
check Checking Debian version...
if [ ! -f "/etc/debian_version" ]; then
red This installer requires a Debian based system.
exit 2
else
white $(</etc/debian_version)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment