Subject: Re: /bin/sh and case and (*^*)
To: Jeremy C. Reed <reed@reedmedia.net>
From: Bruce Korb <Bruce.Korb@gmail.com>
List: tech-userlevel
Date: 04/28/2006 15:37:50
Jeremy C. Reed wrote:

>On NetBSD 3.99.15, I saw an error like:
>
>/usr/pkg/bin/git-fetch: 219: Syntax error: word unexpected (expecting ")")
>
>The error was here:
>
>                        case "$name" in
>                        (*^*) continue ;;
>                        esac
>
>Changing to /bin/ksh to run this continued fine. (I don't know if it works 
>though since I am just beginning using GIT).
>
>I haven't seen a case pattern start with a open parenthesis before. Is 
>this okay?
>
>If so, should /bin/sh support it?
>

This works:

foo=`case "$str" in pat) echo pat ;; esac`

This does not:

foo=$(case "$str" in pat) echo pat ;; esac)

These do (or should):

foo=$(case "$str" in (pat) echo pat ;; esac)
foo=`case "$str" in (pat) echo pat ;; esac`

So, unless you want the syntax of your scripts to vary depending on a 
larger context,
it should be okay to always enclose your patterns in balanced 
parentheses. BTW,
one of the autotool lists (libtool?) raised the issue and nobody could 
come up with
an example where ``${pat})'' would work correctly and ``(${pat})'' would 
not.
It would be interesting to know if there are special circumstances where 
balanced
parentheses fail and just the close parethesis works.

- Bruce