Let's jazz up some Bash scripts today, shall we?
This post was prompted by a forum posting at another site where someone had a script that echoed "Pass' or "Fail" and he wanted "Pass" in green and "Fail" in Red.
By the way, I don't recommend this.
Color is tricky. Displays can be bad at it, people can be color blind... using colors can make text hard to read - I don't like using color in scripts. But, if you insist...
The basic tool for terminal handling is "tput". Put this in a shell script and run it:
For Linux, start up an Xterm or switch to a text console (CTRL-ALT-F1). For Mac OS X, run the Terminal application. You can put these commands in a shell script or just type them.
tput setaf 0;tput setab 7; echo "`tput setaf 1` FAIL `tput setaf 7`" echo "`tput setaf 2` PASS `tput setaf 7`" tput op
Assuming your terminal session can do color, that should have put "FAIL" in red on a white or gray background, "PASS" in green below it and then returned your terminal to its original settings - that is, if you began with a black background and white text, the "tput op" should return you to that, if you had a white background and blue text, you'll be back to that. If for some reason you had a black background with red text, then this didn't change much, did it?
On my screen, both the red and the green are a bit hard to read. I can jazz that up a bit by adding the sequences to start and stop "bold":
echo "`tput setaf 1;tput smso` FAIL `tput rmso;tput setaf 7`"
But it's still not great. If you are tempted to use color in your scripts, I'd suggest doing something like this:
echo "`tput setaf 1` *** `tput op;tput smso`FAIL`tput rmso;
tput setaf 1` *** `tput op`"
And honestly I'd rather leave out the bolding.
You can do much more with tput:
This one needs to be in a shell script. If you don't know how to create a shell script, see Scripting - how to write a shell script.
echo "hello" echo "Input please" tput up tput up tput nd tput nd tput nd tput nd tput nd tput nd read akey
You'll see the script waiting for input right next to the "hello". It does that because "tput up" goes up one line, and "tput nd" is a non-destructive backspace. You can even be explicit:
tput cup 10 50;echo "testing 1 2 3"
puts the output at column 50 of line 10. You might want to do "clear" before running that.
To learn even more about what "tput" can do, see "man terminfo" or "info terminfo" (and if you dislike "info", try "pinfo"). To understand more about terminfo and its cousin "termcap", see Termcap and Terminfo .
If you dislike Linux colorization (I do!), see Controlling Linux colors in vi (vim).

Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Mon Nov 9 08:52:23 2009: NickBarron
That was a little bit of fun :)
Though practical application is probably lacking a little as you say.
Thanks though.
Mon Nov 9 11:44:33 2009: TonyLawrence
C.FA. Johnson (Author of Shell Scripting Recipes http://aplawrence.com/Books/shellscriptingrecipes.html among other things) pointed out that ISO 6429 standard is well supported and that allows you to use
green=$(printf "\033[32m")
normal=$(printf "\033[m")
Full details at http://en.wikipedia.org/wiki/ANSI_escape_code
Sat Jan 2 05:48:12 2010: kvmredy
Check below link for how to colorizing shell scripts artcle
http://bashscript.blogspot.com/2010/01/shell-colors-colorizing-shell-scripts.html
Sun Jan 3 18:22:12 2010: BigDumbDInosaur
http://bcstechnology.net
Color is tricky. Displays can be bad at it, people can be color blind...
Today's hardware generally displays colors with good accuracy. Color-blindness is much more likely to be a problem. Most men will develop some degree of color-blindness as they age, usually the red-green variety. I inherited the much rarer blue-green color-blindness and thus constantly struggle with many websites whose authors insist on using darker blues on top of lighter blues, or light blue text on a white background (this particular site being one such example). Needless to say, there are many sites that I just skip because I don't want to sit here and fiddle with the browser just so I can read something.
As far as using color in BASH scripts (or the BASH prompt itself), I say be sparing. Also, the use of light colored backgrounds will often wash out some colors, rendering the display hard to read (Tony's above examples are such a case). I use a bright green text with a black background on my text displays (reminds me of the good old green-screen days). I definitely avoid putting blues and greens adjacent to each other. <Grin>
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar