Search code examples
pattern-matchingzshglob

ZSH expanded variables in [[ ... ]] do not perform globbing


I have extended_glob set in .zshrc.

This works as expected:

[[ "value" = [a-z]* ]] && echo "globbed"

Prints "globbed".

But this does not:

foo=[a-z]*
[[ "value" = $foo ]] && echo "globbed"

Doesn't print anything.
Why is that and what do I need to set, if anything, in .zshrc to make it function?


Solution

  • You can use

    foo='[a-z]*'
    [[ "value" == $~foo ]] && echo "globbed"
    

    $~foo notation allows globbing in zsh.