Skip to content

Instantly share code, notes, and snippets.

@RonaldJerez
Created November 30, 2016 20:20
Show Gist options
  • Save RonaldJerez/989033517d08232b00d288a5fce216e2 to your computer and use it in GitHub Desktop.
Save RonaldJerez/989033517d08232b00d288a5fce216e2 to your computer and use it in GitHub Desktop.
bash function to style your console output.
# Regular colors
BLACK="\e[0;30m"
RED="\e[0;31m"
GREEN="\e[0;32m"
YELLOW="\e[0;33m"
BLUE="\e[0;34m"
MAGENTA="\e[0;35m"
CYAN="\e[0;36m"
GRAY="\e[0;37m"
WHITE="\e[0;97m"
CLEAR="\e[0m"
# function to style console output
# you can combine, color, style, or make color lighther
# combining of format (bold/underline etc) is not supported
#
# Usage:
# style red "some text"
# style light red "some text"
# style bold yellow "something else"
# style underline light green "green text"
style() {
local format=0
local light=0
while [ $# -gt 2 ]; do
case "$1" in
light) light=1 ;;
bold) format=1 ;;
underline) format=4 ;;
blink) format=5 ;;
invert) format=7 ;;
hidden) format=8 ;;
esac
shift
done
local color=$(echo "$1" | tr '[:lower:]' '[:upper:]') #convert color name to upper
local code="${!color}" # get the code of the color via expansion
(($light)) && code=${code/3/9}
(($format)) && code=${code/0/$format}
printf "$code" # print the style code
[ -n "$2" ] && printf "$2$CLEAR" # print text if available
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment