Skip to content

Instantly share code, notes, and snippets.

@fbender
Last active December 18, 2015 00:09
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 fbender/5694946 to your computer and use it in GitHub Desktop.
Save fbender/5694946 to your computer and use it in GitHub Desktop.
Simple helper function for the lazy to colorize terminal scripts. MIT License. Color codes from https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Advanced_prompts
#!/bin/bash
# Copyright (c) 2013 Florian Bender
# License: MIT (see http://opensource.org/licenses/MIT)
function colorset {
style=""
case $1 in
text) style="0;3" ;; ## regular text
HItext) style="0;9" ;; ## high intensity regular text
bold) style="1;3" ;; ## bold text
HIbold) style="1;9" ;; ## high intensity bold
undl) style="4;3" ;; ## underline
back) style="4" ;; ## background color
HIback) style="0;10" ;; ## high intensity background color
esac
color=""
case $2 in
black) color="0" ;;
red) color="1" ;;
green) color="2" ;;
yellow) color="3" ;;
blue) color="4" ;;
purple) color="5" ;;
cyan) color="6" ;;
white) color="7" ;;
esac
if [ -n "$style" -a -n "$color" ]
then
echo -en "\e[${style}${color}m"
else
echo -en "\e[0m"
fi
}
@fbender
Copy link
Author

fbender commented Jun 2, 2013

Use case:

source colorset.sh

colorset bold green
echo "Wow, I'm bold and green!"
colorset 
echo "I'm just plain and normal."

If an invalid keyword for either $style or $color is provided, the style will be reset to the default.

Tip: echo -n TEXT will suppress the newline character at the end of the statement, so you can use different formats on one line.

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