Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test glob against an arbitrary string in zsh?

Tags:

zsh

glob

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.

like image 751
sasquires Avatar asked Sep 06 '25 03:09

sasquires


1 Answers

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

if [[ string == s* ]]; then
   echo string starts with s
fi
like image 60
chepner Avatar answered Sep 07 '25 21:09

chepner