Colored Output in Bash Function

By sterlo in Bash on 24/02/2009 at 22:45

Views: 1122
Tagged: bash, color
URL: http://www.sterlinghamilton.com/blog/article/using_color_text_in_bash_scripts/

Starter:

Simple bash function for adding color to output.

Main Course:

#!/bin/bash
#
# Example usage:
# echo ${RedF}This text will be red!${Reset}
# echo ${BlueF}{$BoldOn}This will be blue and bold!${BoldOff} - and this is just blue!${Reset}
# echo ${RedB}${BlackF}This has a red background and black font!${Reset}and everything after the reset is normal text!
 
Colors() {
  Escape="\033";
 
  BlackF="${Escape}[30m";   RedF="${Escape}[31m";   GreenF="${Escape}[32m";
  YellowF="${Escape}[33m";  BlueF="${Escape}[34m";    Purplef="${Escape}[35m";
  CyanF="${Escape}[36m";    WhiteF="${Escape}[37m";
 
  BlackB="${Escape}[40m";     RedB="${Escape}[41m";     GreenB="${Escape}[42m";
  YellowB="${Escape}[43m";    BlueB="${Escape}[44m";    PurpleB="${Escape}[45m";
  CyanB="${Escape}[46m";      WhiteB="${Escape}[47m";
 
  BoldOn="${Escape}[1m";      BoldOff="${Escape}[22m";
  ItalicsOn="${Escape}[3m";   ItalicsOff="${Escape}[23m";
  UnderlineOn="${Escape}[4m";     UnderlineOff="${Escape}[24m";
  BlinkOn="${Escape}[5m";   BlinkOff="${Escape}[25m";
  InvertOn="${Escape}[7m";  InvertOff="${Escape}[27m";
 
  Reset="${Escape}[0m";
}

Expand Report Code | Install Coda Clip

Please log in to vote.

Compliments to the Chef

No comments yet...

How was your Meal?


  • Allowed HTML: <b><strong><u><i><em><a>

  • Why ask? It helps us stop spam comments.