zsh-workers
 help / color / mirror / code / Atom feed
* pattern matching, when the pattern is a parameter expansion
@ 2021-02-03 15:51 Greg Klanderman
  2021-02-03 16:03 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Klanderman @ 2021-02-03 15:51 UTC (permalink / raw)
  To: Zsh list


Hi,

I seem to have found an inconsistency with pattern matching,
when the pattern is a parameter expansion:

% z=(one two three)

in three syntactic forms that accept a pattern, the pattern can be the
result of a parameter expansion:

% x=two
% [[ two == $x ]] && echo yes || echo no
yes
% echo "${(@)z:#$x}"
one three
% echo "${z[(i)$x]}"
2

now, when x contains an actual pattern rather than a simple string, it
is not matched in the first two cases, but is in the final case:

% x='t*'
% [[ two == $x ]] && echo yes || echo no
no
% echo "${(@)z:#$x}"
one two three
% echo "${z[(i)$x]}"
2

I would have actually expected to match in the first two cases as
well; is this behavior of using plain string matching when the pattern
is a parameter expected documented?

(I know I can get that behavior with the 'e' subscript flag in the
final case)

There is some explanation of pattern matching in Chapter 12
(Conditional Expressions) which seems relevant but not clear on these
cases:

| Normal shell expansion is performed on the file, string and pattern
| arguments, but the result of each expansion is constrained to be a
| single word, similar to the effect of double quotes.
..
| Pattern metacharacters are active for the pattern arguments; the
| patterns are the same as those used for filename generation, see
| Section 14.8 [Filename Generation], page 73, but there is no special
| behaviour of ?/? nor initial dots, and no glob qualifiers are allowed.


thank you,
Greg


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-02-03 18:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-03 15:51 pattern matching, when the pattern is a parameter expansion Greg Klanderman
2021-02-03 16:03 ` Peter Stephenson
2021-02-03 18:30   ` Greg Klanderman

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).