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

pattern matching, when the pattern is a parameter expansion



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




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