Skip to content

Instantly share code, notes, and snippets.

@inexorabletash
Created February 20, 2014 20:30
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inexorabletash/9122583 to your computer and use it in GitHub Desktop.
Save inexorabletash/9122583 to your computer and use it in GitHub Desktop.
Bash color utilities
#!/usr/bin/env bash
# Usage:
# echo -e "... $(text-style style ...) ..."
# echo -e "... $(text-reset) ..."
# echo -e "... $(text-color r g b) ..."
# echo -e "... $(background-color r g b) ..."
#
# Where:
# styles:
# black red green yellow blue magenta cyan white default
# (prefix with 'bg' for background color)
# bold faint normal
# italic roman
# underline nounderline
# inverse negative positive
# conceal reveal
# strike nostrike
# xterm N (two consecutive arguments)
# bgxterm N (two consecutive arguments)
# reset
# r/g/b in 0...5 for color triplets
#
# Example:
# echo -e "This is $(text-color 5 5 0) yellow $(text-reset)"
# echo -e "This is $(text-style bold blue underline) fancy $(text-reset)"
function text-style {
while [ $# -gt 0 ]; do
case "$1" in
reset) echo -ne "\E[0m" ;; # terminal default
bold) echo -ne "\E[1m" ;;
faint) echo -ne "\E[2m" ;; # not widely supported (gnome-terminal does)
italic) echo -ne "\E[3m" ;; # not widely supported
underline) echo -ne "\E[4m" ;;
blink) echo -ne "\E[5m" ;; # not widely supported
blinkfast) echo -ne "\E[6m" ;; # not widely supported
negative) echo -ne "\E[7m" ;;
conceal) echo -ne "\E[8m" ;; # not widely supported (gnome-terminal does)
strike) echo -ne "\E[9m" ;; # not widely supported (gnome-terminal does)
normal) echo -ne "\E[22m" ;; # cancel bold/faint
roman) echo -ne "\E[23m" ;; # cancel italic
nounderline) echo -ne "\E[24m" ;; # cancel underline
noblink) echo -ne "\E[25m" ;; # cancel blink
positive) echo -ne "\E[27m" ;; # cancel negative
reveal) echo -ne "\E[28m" ;; # cancel conceal
nostrike) echo -ne "\E[29m" ;; # cancel strike
black) echo -ne "\E[30m" ;;
red) echo -ne "\E[31m" ;;
green) echo -ne "\E[32m" ;;
yellow) echo -ne "\E[33m" ;;
blue) echo -ne "\E[34m" ;;
magenta) echo -ne "\E[35m" ;;
cyan) echo -ne "\E[36m" ;;
white) echo -ne "\E[37m" ;;
xterm) echo -ne "\E[38;5;$2m" ; shift ;;
default) echo -ne "\E[39m" ;;
bgblack) echo -ne "\E[40m" ;;
bgred) echo -ne "\E[41m" ;;
bggreen) echo -ne "\E[42m" ;;
bgyellow) echo -ne "\E[43m" ;;
bgblue) echo -ne "\E[44m" ;;
bgmagenta) echo -ne "\E[45m" ;;
bgcyan) echo -ne "\E[46m" ;;
bgwhite) echo -ne "\E[47m" ;;
bgxterm) echo -ne "\E[48;5;$2m" ; shift ;;
bgdefault) echo -ne "\E[49m" ;;
*)
echo -"ne \E[0m"
echo "Unknown code: $token" 1>&2
exit 1
;;
esac
shift
done
}
function text-color {
text-style xterm $(expr 16 + 36 \* $1 + 6 \* $2 + $3)
}
function background-color {
text-style bgxterm $(expr 16 + 36 \* $1 + 6 \* $2 + $3)
}
function text-reset {
text-style reset
}
#!/usr/bin/env bash
set -e
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $dir/ansi_colors
############################################################
codes=''
opts='-e'
function usage() {
sample="ABCabc123!@#"
echo "Usage: cecho [options] ..."
echo "Options:"
echo " -n No trailing newline"
echo " --color=COLOR Foreground color"
echo " --bgcolor=COLOR Background color"
echo " --STYLE Style (or foreground color e.g. --red)"
echo "Colors are:"
for i in black red green yellow blue magenta cyan white default; do
echo -e " $(printf '%-10s' $i) $(text-style $i)$sample $(text-style bold)$sample $(text-reset)$(text-style bg$i)$sample$(text-reset)"
done
echo "Styles are:"
for i in bold faint underline negative conceal strike; do
echo -e " $(printf '%-10s' $i) $(text-style $i)$sample$(text-reset)"
done
}
while [ $# -gt 0 ]; do
case $1 in
--help)
usage
exit 0
;;
--)
shift
break
;;
-n)
opts="$opts -n"
shift
;;
--color\=*)
color=${1#*=}; shift
codes="$codes$(text-style $color)"
;;
--bgcolor\=*)
color=${1#*=}; shift
codes="$codes$(text-style bg$color)"
;;
--*)
color=${1#--}; shift
codes="$codes$(text-style $color)"
;;
*)
break
;;
esac
done
echo $opts $codes"$@"$(text-reset)
@ppo
Copy link

ppo commented Apr 4, 2020

Hi, I found this when looking info on that topic… then I had an idea… and it ended up in a quite nice function extremely easy to use :-D

echo -e "$(c 0wB)Bold white$(c) and normal"
echo -e "Normal text… $(c r_yB)BOLD red text on yellow background… $(c _w)now on white background… 
$(c 0U) reset and underline… $(c) and back to normal."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment