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

Re: pattern matching, when the pattern is a parameter expansion



> On 03 February 2021 at 15:51 Greg Klanderman <gak@xxxxxxxxxxxxxx> wrote:
> % 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?

To cut a long story short: the third case is actually the odd one out.
Subscripts always treat the argument as a pattern --- the basic doc
for that is the under the "r" subscript flag, to which "i" refers.
(I find this somewhat inconvenient as it makes subscript arguments
hard to quote.)

Otherwise, substituted pattern characters are not active by default:
see the GLOB_SUBST option.  You can turn this on with a "~" after
the "$", see the description of ${~spec} in the zshexpn manual.

% x='t*'
% [[ two == $~x ]] && echo yes || echo no
yes

pws




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