# # Using color in shell scripts
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

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

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© November 2009 Anthony Lawrence

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.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





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

Printer Friendly Version

->
-> Using color in shell scripts

5 comments


Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

El Capitan: A Take Control Crash Course

Take Control of Numbers

Digital Sharing Crash Course

Take Control of iCloud





More Articles by © 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>

------------------------


Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





Show me your flowchart and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won't usually need your flowchart; it'll be obvious. (Fred Brooks, The Mythical Man Month)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Linux

Scripting

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode