Skip to content

Instantly share code, notes, and snippets.

@Yaulendil
Created December 19, 2020 20:36
Show Gist options
  • Save Yaulendil/7abb63a2837c42f6dd5e4f3d1eecc409 to your computer and use it in GitHub Desktop.
Save Yaulendil/7abb63a2837c42f6dd5e4f3d1eecc409 to your computer and use it in GitHub Desktop.
Bash Function for easy ANSI color codes
function color() {
reset=(reset)
if ( [[ -z "$1" ]] || [[ "$@" == "$reset" ]] ); then
echo -en "\e[0m"
else
style=22
class=3
color=0
for term in "$@"; do
case "$term" in
normal) style=22 ;;
bold) style=1 ;;
dim) style=2 ;;
italic) style=3 ;;
uline) style=4 ;;
blink) style=5 ;;
blink2) style=6 ;;
invert) style=7 ;;
hide) style=8 ;;
strike) style=9 ;;
normal) class=3 ;;
back | bg) class=4 ;;
bright | light)
class=9 ;;
bg-bright | bg-light)
class=10 ;;
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
purple) color=5 ;;
cyan) color=6 ;;
white) color=7 ;;
clear)
style=22
class=3
color=0
;;
esac
done
echo -en "\e[${style};${class}${color}m"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment