Diary of a Network Geek

The trials and tribulations of a Certified Novell Engineer who's been stranded in Houston, Texas.

1/20/2005

Adding Color to BASH menus

Filed under: Art,Fun Work,Geek Work,Linux,Novell,Things to Read — Posted by the Network Geek during the Hour of the Rooster which is in the early evening or 6:39 pm for you boring, normal people.
The moon is Waning Gibbous

I know I’ve been sort of quiet this week, so here’s a tidbit.
I’ve been doing a LOT of bash scripting lately for the ZENWorks imaging project that I’ve been working on. Well, the first part of that goes live, or partially live, next week. Yikes! So, I’ve been refining and getting things ready for that. Busy, busy, busy! But, I did have time to notice someone asking for help adding color to the menus and such on a ZENWorks Discussion Forum at Novell. I posted a reply there, but I thought I’d do up something for the oh, so quiet blog, too.
First off, all this comes from the FAQs.org, Advanced BASH-scripting Guide, in the Colorizing Scripts section.

Now, remember, these are basically just escape sequences, just like from the old DOS days and batch files. (Thought you’d never have to do that again, right?) Keep in mind that once you apply one of these codes, it will be in effect until you cancel it out, one way or another.
Let’s start easy with a simple bold:
echo -e “33[1mThis line is bolded33[0m”;
Okay, notice that you need to append the ” -e” to your standard “echo” command for display. The actual “bold” code is “33[1m”. That’s it. Just add that to the begining of the quoted line and you’ve got bolded text. To “turn it off”, simply echo “33[0m”, either at the end of the quoted line as above, or on another line. Personally, I find it easier to turn off my codes ASAP. It cuts down on problems later on.
Now, we’ll make green, bold text:
echo -en “33[1mThis is bold and green”
tput sgr0

Okay, now notice that we “turned off” the codes with a different command. That command, tput sgr0, clears all the color and formatting.
Now, to do one color for the text and one for the background, do this:
echo -e ‘\E[34;47m'”This makes blue text on a white background”;

And, that’s really all there is to it. So, go experiment with your bash colors!

Here’s a table of some other colors for your reference:

Color Foreground Background
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47

Tags:

Powered by WordPress
Any links to sites selling any reviewed item, including but not limited to Amazon, may be affiliate links which will pay me some tiny bit of money if used to purchase the item, but this site does no paid reviews and all opinions are my own.