possible wrong behaviour with patterns using a quoted ^ at the start of a bracket expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey.


I recently got some big deal of help from the people at the help-bash
mailing list when I've tried to understand what POSIX mandates with
respect to pattern matching (that is in the sense of [0], not
Basic/Extended Regular Expressions).

I'm still not so sure whether I understand it exactly by the wording of
POSIX itself (which seems a bit odd to me), but what people
explained[1] me at help-bash - and I hope I explain it correctly - is:

In the patterns, even in a bracket expression in a pattern, there may
be quoting (with double and single quotes), and - and this is key -
anything that is quoted is already taken literal with respect to the
pattern.

So when one has e.g. a case compound command:
case $foo in
	(['*?'])
...
is already the literal * and ? within a pattern's bracket expression.


Further, POSIX says:
"If an open bracket introduces a bracket expression as in XBD RE
Bracket Expression, except that the <exclamation-mark> character ( '!'
) shall replace the <circumflex> character ( '^' ) in its role in a
non-matching list in the regular expression notation, it shall
introduce a pattern bracket expression. A bracket expression starting
with an unquoted <circumflex> character produces unspecified results.
Otherwise, '[' shall match the character itself."



I found not the following probably wrong behaviour of dash and busybox'
sh:




$ cat circumflex-test 
case "$1" in
(['^.a'])
 echo match
 ;;
(*)
 echo else
esac


$ cat exclamation-test 
case "$1" in
(['!.a'])
 echo match
 ;;
(*)
 echo else
esac


$ cat run-circumflex 
echo dash:
dash circumflex-test ^
dash circumflex-test .
dash circumflex-test a

echo busybox-sh:
busybox sh circumflex-test ^
busybox sh circumflex-test .
busybox sh circumflex-test a

echo bash:
bash circumflex-test ^
bash circumflex-test .
bash circumflex-test a

echo klibc-sh:
/usr/lib/klibc/bin/sh circumflex-test ^
/usr/lib/klibc/bin/sh circumflex-test .
/usr/lib/klibc/bin/sh circumflex-test a


$ cat run-exlamation 
echo dash:
dash exclamation-test '!'
dash exclamation-test .
dash exclamation-test a

echo busybox-sh:
busybox sh exclamation-test '!'
busybox sh exclamation-test .
busybox sh exclamation-test a

echo bash:
bash exclamation-test '!'
bash exclamation-test .
bash exclamation-test a

echo klibc-sh:
/usr/lib/klibc/bin/sh exclamation-test '!'
/usr/lib/klibc/bin/sh exclamation-test .
/usr/lib/klibc/bin/sh exclamation-test a




When run:
$ sh run-circumflex | paste - - - - | column -t
dash: match else else
busybox-sh: match else else
bash: match match match
klibc-sh: match match match
$
 ^ . a


$ sh run-exlamation | paste - - - - | column -t
dash: match match match
busybox-sh: match match match
bash: match match match
klibc-sh: match match match
$
 ! . a


The results for the run-circumflex seem pretty odd.
Apparently, the ^ is taken literally, but the other two are negated.

$ dash circumflex-test b
match
$ busybox sh circumflex-test b
match

match again (which, AFAIU, they should not).

While POSIX does say:
"A bracket expression starting with an unquoted <circumflex> character
produces unspecified results."
... the circumflex *is* quoted above...


I haven't verified any further unusual patterns like ['$var'] vs.
["$var"], so maybe an eye should be kept open, whether there could be
any issues as well.


Thanks,
Chris.

PS: For reference, the bug[2] I've opened at BusyBox.



[0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13
[1] https://lists.gnu.org/archive/html/help-bash/2022-01/msg00000.html
[2] https://bugs.busybox.net/show_bug.cgi?id=14516




[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux