Subject: Re: bin/4020: Globbing error in /bin/sh
To: None <ian.dall@dsto.defence.gov.au>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: netbsd-bugs
Date: 08/20/1997 12:20:05
> arg='&foo'
> case "$arg" in
>    *[\[\]\&]*)

That expression matches
	*	zero or more characters
	[\[\]	same as [[] - an open bracket
	\&	an &
	]	a close bracket
	*	zero or more characters
which does not match "&foo".

I wrote a test script

#! /bin/sh
arg='&foo'
echo "arg is $arg"
case "$arg" in
	*[\[\]\&]*)
		echo matches 1
		;;
	*[][\&]*)
		echo matches 2
		;;
	*)
		echo no match
		;;
esac

and it produced "matches 2".

There may be a bug, but if so it's in quoting, not matching.  ] is not
considered to be a character that needs quoting, so attempts to quote
it get stripped, and by the time it makes it down to the routine that
matches [...] character classes in glob patterns, any record of it
having been quoted has been destroyed.  Read the "Shell Patterns"
section in the sh manpage (at the top of page 19); ] is not a
meta-character - or, to put it another way, a character class opened by
[ extends up to the next ] *regardless of whether the ] is quoted*
(with a special case to allow putting ] into a class by listing it
first).

Incidentally, anyone know what possessed anyone to use ! for negating a
character class, when everything else in the known universe uses ^ for
that function?

Anyone have a copy of the POSIX spec handy to say whether there really
is any bug here, and if so what?

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B