I'm using the =~
in one of my Bash scripts. However, I need to make the script compatible with Bash versions that do not support that operator, in particular the version of Bash that ships with msysgit which is not build against libregex. I have a work-around that uses expr match
instead, but that again does not work on Mac OS X for some reason.
Thus, I want to use the expr match
only if [ -n "$MSYSTEM" -a ${BASH_VERSINFO[0]} -lt 4 ]
and use =~
otherwise. The problem is that Bash always seems to parse the whole script, and even if msysgit Bash would execute the work-around at runtime, it still stumbles upon the =~
when parsing the script.
Is it possible to conditionally execute code in the same script depending on the Bash version, or should I look into another way to address this issue?
My current solution is to use grep -q
on all platforms instead. This avoids any conditionals or complicated code constructs.
Probably using eval
to parse the code containing =~
only at runtime would have worked, too, but then again that would have made the code more complicated to read.