help-bash
[Top][All Lists]
Advanced

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

help with pattern matching needed


From: Christoph Anton Mitterer
Subject: help with pattern matching needed
Date: Fri, 07 Jan 2022 06:28:30 +0100
User-agent: Evolution 3.42.2-1

Hey.

I'd need some help with pattern matching (in the sense of patterns as
in the case compound command's patterns, respectively [0], not as in
BRE or ERE).


When I have:
case "${character}" in
(pattern)
        foo
        ;;
(*)
        bar
esac

and I want to match different patterns, than AFAIU, the pattern
undergoes quote removal, first right?



Case 1:
*******
A string like:
\\
would end up a s the pattern
\
which by itself is apparently already taken as the literal \ ?!

Why don't I have to quote that again (for the pattern)?

POSIX says:
"A <backslash> character shall escape the following character. The
escaping <backslash> shall be discarded. If a pattern ends with an
unescaped <backslash>, it is unspecified whether the pattern does not
match anything or the pattern is treated as invalid."

Instead, using:
\\\\
or
'\\'
doesn't produce what I'd expect by the above, but matches the
literal \\ .



Case 2:
*******
A string like:
[.*^$[\]
should end up (after quote removal) as the pattern:
[.*^$[]
an AFAIU be valid (but of course not match the literal \), but bash
complains about a missing matching ].
That construct executes e.g. in (d)ash, though it never matches (or at
least not with the plain single characters).



Case 3:
*******
When I however remove the \ the string:
[.*^$[]
should also end up as the pattern:
[.*^$[]
an AFAIU be valid, but while that executes, neither of these characters
match and I always end up in the (*) case.

Any ideas why?

I stumbled over the old $[] form of arithmetic expression, but I guess
that cannot be it, cause then one would again have a missing matching
].

btw: That case also works as (I'd have expected it) in (d)ash, i.e. it
matches the single characters of the pattern including the literal [



Case 4:
*******
Using the string:
['.*^$[\']
which should end up as the pattern:
[.*^$[\]
works as expected, and \ which inside a bracket expression has no
special meaning is also matched as it should

Same for the string:
['.*^$[']
ends up as the pattern:
[.*^$[]
and matches as it should (without the literal \)



Thanks for your insights,
Chris.

PS: In another thread of mine[1] some similar things came up in the
past already, but from the answers that were given there I still don't
understand the above O:-)



[0] 
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13
[1] https://lists.gnu.org/archive/html/help-bash/2021-05/msg00140.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]