Search code examples
zshglob

Test glob against an arbitrary string in zsh?


Is it possible to test whether a string matches a glob in zsh? This would be completely independent of file paths. Specifically, I am wondering whether there is a command (call it testglob) that allows me to check whether a string would be considered a match to the glob, like so:

testglob '^s*' string_that_matches
testglob '^s*' does_not_match

I would expect different return values for these two commands.

If this does not already exist, it would be a great feature to consider for addition. It would allow external applications to access zsh's globbing system without having to emulate its globbing rules.


Solution

  • The [[ ... ]] command does this, with the regular == operator.

    if [[ string == s* ]]; then
       echo string starts with s
    fi