[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: incompatibility in regex
From: |
Greg Wooledge |
Subject: |
Re: incompatibility in regex |
Date: |
Wed, 28 Mar 2012 14:49:24 -0400 |
User-agent: |
Mutt/1.4.2.3i |
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.