Using color in shell scripts (Linux, Mac OS X)

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) and Linux ls colors.



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Using color in shell scripts

5 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Mon Nov 9 08:52:23 2009: 7503   NickBarron

gravatar
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: 7504   TonyLawrence

gravatar
C.FA. Johnson (Author of Shell Scripting Recipes (link) 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 (link)









Sat Jan 2 05:48:12 2010: 7844   kvmredy

gravatar


Check below link for how to colorizing shell scripts artcle
(link)



Sun Jan 3 18:22:12 2010: 7847   BigDumbDInosaur

gravatar


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>

------------------------
Kerio Samepage


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.

Contact us





The history of the world teaches us that succession is dangerous and that the strong take what they want. It's not likely to be any different with Linux. (Tony Lawrence)





This post tagged: