On Mon, Oct 10, 2011 at 08:06:17PM +0200, Peter Schreiber wrote:
> 3.1.17(1)-release:
> 
> -> [[ a =~ a|b ]] && echo true
> -bash: syntax error in conditional expression: unexpected token `|'
> 
> -> [[ a =~ a\|b ]] && echo true   # that one works in version 3
> true
> 
> ===================================================================
> 
> 4.1.10(4)-release:
> 
> -> [[ a =~ a\|b ]] && echo true   # ... but not in version 4
> 
> -> [[ a =~ a|b ]] && echo true
> true
> 
> 
> Do I really need to check BASH_VERSION first?

Always store the regular expression that you intend to use on the
right hand side of =~ in a variable.

re='a|b'
if [[ $a =~ $re ]]; then ...

That is the only way to be sure it will work in whatever version of bash
you happen to be using.

Reply via email to