== Matching words against a list in the Bourne Shell One of the interesting challenges in writing shell scripts is figuring out how to do as much as is practical with shell builtins and primitives. Since Bourne shell builtins are such a limited and constrained environment, the results can be somewhat perverse and peculiar and thus neat and amusing. (If you can find an efficient way of doing things, using builtins is much faster than having to resort to external programs, especially when the system is loaded.) My challenge today was checking whether a word was in a list of words (words don't have spaces), in a Bourne shell dialect that would work as far back as Solaris 2.5. What I came up with is: > case "$wlist" in > $w|"$w "*|*" $w "*|*" $w") echo yes;; > *) echo no;; > esac (Where _$w_ is the word and _$wlist_ is the space-separated list of words to match against. You need the first match condition to deal with the case where _$wlist_ consists only of _$w_.) I sometimes feel I have an advantage in this sort of perversity, because I started out writing shell scripts in a version of the Bourne shell that was so old that it didn't even have _test_ (aka _[ ... ]_) as a builtin. When you don't have a built in _test_ you get a *lot* of experience with abusing _case_, because it is basically the only builtin testing operation you have. (To this day I have a tendency to use _case_ for condition tests where I really should use _if_. Something in the back of my mind is still not really convinced that things like '_if [ "$#" -eq 0 ]_' don't require an external program.)