[CALUG] Shell script question.

Bryan J. Smith b.j.smith at ieee.org
Tue May 4 16:36:06 EDT 2010

There is still a place, time and reason for writing POSIX Bourne Shell (sh),
and not using Bourne Again Shell (bash) specifics.

A big one for myself has been the fact that different bash versions exist,
and they don't all act alike either (let alone have some bugs).

Bryan J  Smith             Professional, Technical Annoyance 
Linked Profile:           http://www.linkedin.com/in/bjsmith 
"Now if you own an automatic ... sell it!
"You are totally missing out on the coolest part of driving"
-- Johnny O' Connell

----- Original Message ----
From: James Ewing Cottrell 3rd <JECottrell3 at Comcast.NET>

Everything said so far has been true, however, as far as I am concerned 
it completely misses the point, and illustrates one of my Pet Peeves.

*** Avoid using IF to test for String (in)equality or Patterns.
*** Use CASE instead

case "$var" in
('')    echo VAR is empty;;
(*)    echo VAR is nonempty;;

This is MUCH easier to read, and you get to use Filename Globbing 
characters as well.

I prefer the extra left parenthesis to keep the emacs paren balancer 
happy. Otherwise, you need "# (" comments on the first two lines.

I don't worry about older shells much; if I was stuck on such a host, 
I'd compile my own bash, altho leaving the '(' out is certainly a 
possibility too.

Modern shells tend to treat the results of a variable as a "word", altho 
older shells may have reparsed the output. I'd still use the double 
quotes tho, just to make it explicit.

More information about the CALUG mailing list