tags:

views:

48

answers:

2

I want to use a feature only present in newer versions of zsh: [[ "$foo" =~ "regexp" ]] where regexp is a regular expression.

Is it possible to do this check?

Something like:

if [[ $ZSH_VERSION > 4.3.9 ]]; then
   ....
fi

Except that > won't work.

Ciao!

A: 

I tried

if [[ $ZSH_VERSION > 4.3.8 ]]; then
  echo "YES"
fi;

and it worked. In my case $ZSH_VERSION has a value of 4.3.9, so I had to check for a different version.

kiamlaluno
This is probably not reliable. My ZSH_VERSION is 4.3.10-dev-1, and `[[ $ZSH_VERSION > 4.3.8 ]]` returns false (because “10” comes before “8” in a string comparison).
Chris Johnsen
I think the problem is the `-dev-1` part.
kiamlaluno
No, `[[ 4.3.10 > 4.3.8 ]]` is also false. The `>` operator does a string comparison.
Chris Johnsen
You are right. I am used to compare versions strings in PHP, which does the right thing. The solution you found is probably the unique one.
kiamlaluno
+3  A: 

I just found is-at-least but I'm not sure if it is available in all/many versions of zsh.

Usage:

autoload -U is-at-least
if is-at-least 4.3.9; then
   ....
fi

It's described in zshcontrib.

Ciao!

The Doctor What
According to the ChangeLog and the Git mirror of the CVS history, the first version of `is-at-least` dates back to 2000-02-11 and was first released in either 3.1.6-dev18 or 3.1.6-dev21.
Chris Johnsen
Awesome! Do you have a link to these resources?
The Doctor What