When a script or program looks like your
cat ran across the keyboard, it might be very
efficient, but it’s sure hard to debug later,
even if it’s your own code.
test the scripts in this column against
Bourne Shell (not being Jason, after
all), and well, just because I’m not
using your favorite Bash features and
shortcuts, that doesn’t mean I’m using
that “other” shell, does it?
The most valuable part in the letter
was to remind me that there are some
slick notational conventions that are
added to modern Bash shells that
can clean up some of our conditional
statements and structures. It was a
good reminder—old dog, new tricks,
and all that. Let’s have a look.
Shortening Conditional Tests
One of the first programming languages
I learned was APL. You probably
haven’t even heard of it, but it was
a remarkably powerful language
characterized by lots of special
notations that gave you the ability to
produce sophisticated results in just
a line or two. The problem was, no
one could debug it, and the common
belief was that it was faster to rewrite
a program than to figure out what
another programmer was thinking.
if [ -n $value ] ; then
What I haven’t explained is that every
time you write a conditional in this form,
it actually invokes a subshell process to
solve the equation. Write it with double
if [[ -n $value ]] ; then
and you’ll force the test to remain
within the shell itself, which will make
your scripts faster and more efficient.