web analytics

Unix: print in color

This simple procedure takes 3 arguments as the parameters:

  • Text
  • Foreground color
  • Background color

It prints the text to the terminal in selected colors.

function print_color {
 local text=$1
 local fg=$2
 local bg=$3
 case "$fg" in
 red) fg="31m" ;;
 green) fg="32m" ;;
 yellow) fg="33m" ;;
 blue) fg="34m" ;;
 white) fg="37m" ;;
 black) fg="30m" ;;
 *) fg="37m" ;;
 esac
 case "$bg" in
 red) bg="41m" ;;
 green) bg="42m" ;;
 yellow) bg="43m" ;;
 blue) bg="44m" ;;
 white) bg="47m" ;;
 black) bg="40m" ;;
 *) bg="40m" ;;
 esac
 echo -en "\033[${fg}\033[${bg}${text}\033[0m"
}

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.