Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: strange glob parsing



On Mon, Jun 2, 2025, at 9:42 PM, Ryan Rotter wrote:
> I encountered an edge case in the parsing of globs, and I'm wondering
> whether this behavior is a bug, or if this is known/expected:
>
> /bin % ls [^]
> [
> /bin % ls [^][^]
> zsh: no matches found: [^][^]
> /bin % ls [^][^][^][^][^]
> cat     csh     ksh     pax     pwd     zsh
>
> It appears that:
> [^] alone is parsed as a character class of "all chars" (the same as
> ?), which is exactly what I expected,

It's not what I expect.  I believe that POSIX requires the synonymous
"[!]" to match a literal '[' followed by a literal '!' followed by
a literal ']', and bash, dash, and ksh perform matching this way.
(Of course, zsh is not obliged to follow suit; I'm just noting that
it's marching to the beat of its own drum here.)


> but [^][^] is parsed as "all chars except ],[, and ^", rather than acting
> the same as ?? as I was expecting,
> and n repeated [^] in a row matches n/2 + n%2 chars, (with the last
> char matching anything, and the rest matching anything except ],[, and
> ^).

These are expected.  In each of "[]...]", "[!]...]", and "[^]...]",
the leading ']' represents itself in the list of candidate characters
and doesn't preemptively terminate the bracket expression.

	A `-' or `]' may be matched by including it as the first
	character in the list.

https://zsh.sourceforge.io/Doc/Release/Expansion.html#Glob-Operators


> If [^] isn't consistently equivalent to ?, it would be less astonishing
> if it was simply an error.

There are other possible interpretations that would not be erroneous.


> So, is this a bug, or did I just "discover"
> a quirk that is known or maybe even expected?

The behavior of "[^][^]" and "[!][!]" is quite intentional, but
I don't know whether the behavior of "[^]" and "[!]" is.


-- 
vq




Messages sorted by: Reverse Date, Date, Thread, Author