From: Martijn Dekker <martijn@inlv.org>
To: zsh-workers@zsh.org
Subject: Pattern matching: backslash escaping not active in pattern passed from variable
Date: Mon, 18 May 2015 04:07:40 +0100 [thread overview]
Message-ID: <5559577C.1060906@inlv.org> (raw)
Peter Stephenson's patch for case pattern matching applied cleanly
against the latest git code and is working perfectly. Many thanks.
There is another snag I've found in pattern matching, which seems to
have survived this patch: backslash escaping does not seem match the
standard if a glob pattern is passed from a variable or parameter. The
standard says:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_01
> A <backslash> character shall escape the following character. The
> escaping <backslash> shall be discarded.
This works correctly with a literal pattern:
case abc in ( a\bc ) echo yes ;; ( * ) echo no ;; esac
Got expected output: yes
But it does *not* work correctly if the pattern is in a variable or
parameter:
glob='a\bc'
case abc in ( $glob ) echo yes ;; ( * ) echo no ;; esac
Expected output: yes (produced by bash, ksh93, dash)
Got output: no (produced by zsh, mksh, ash)
To be fair, existing implementations seem wildly inconsistent here, so
perhaps I'm misreading something. In any case, the inconsistency is
unfortunate. I like to use this simple function so I can write something
like: if match "$somestuff" '*stuff*'; then [...]
match() {
case "$1" in ( $2 ) return 0 ;; esac
return 1
}
But the inconsistent handling of backslash escaping in glob patterns
passed from variables or parameters makes it impossible to portably
match a literal '*' or '?' using a method like this. mksh and ash never
match a literal escaped \* and \?; oddly enough, zsh does, but does not
match a (redundantly) escaped alphanumeric character.
In any case, I would expect backslash escaping to work the same way
whether the glob pattern is passed from a variable or parameter or not.
Thanks,
- Martijn
next reply other threads:[~2015-05-18 3:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-18 3:07 Martijn Dekker [this message]
2015-05-18 8:33 ` Mikael Magnusson
2015-05-18 8:49 ` Peter Stephenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5559577C.1060906@inlv.org \
--to=martijn@inlv.org \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).