in f13 =~ no longer working in bash

James Findley sixy at
Wed Jun 9 09:08:29 UTC 2010

On 06/08/2010 08:05 PM, Richard W.M. Jones wrote:
> On Tue, Jun 08, 2010 at 02:14:12PM +0200, Farkas Levente wrote:
>> hi,
>> =~ no longer working in bash. just try this little line:
>> -----------------------------
>> if [[ "abc" =~ "abc.*" ]]; then echo inside; else echo outside; fi
>> -----------------------------
>> this give "inside" up to fedora-12, but it gives
>> "outside" in fedora-13.
>> imho it's a serious changes since all shell script will fail which use
>> =~ :-(
>> is there any reason for this? or any quick fixes?
> This bit us in libguestfs too, where we'd used the bash extension
> without thinking that they'd break it on us.
> The full fix is *not* just quote removal.
> cf. our first fix:
> and out later, complete fix:
> Huge pain in the neck .. bash should retain backwards compatibility,
> even for non-POSIX extensions.
> Rich.

This actually stems from using poor bash syntax in the first place. 
Inside [[ foo ]] (as opposed to [ foo ]) you should not quote parameters.

No wordsplitting or glob expansion is done inside [[ ]], so there is no 
need for those quotes.  Get rid of them, and woosh! it works!

For example, from your script, you had:
[[ "$path" =~ '^\./etc' || "$path" =~ '^./dev' || "$path" =~ '^\./var' ]]
If you had written this properly as:
[[ $path =~ ^\./etc || $path =~ ^\./dev || $path =~ ^\./var ]]
It would have continued to work just fine.

The old behaviour may have worked on older bash versions, but it was 
always undefined, and relying on undefined behaviour in any programming 
language is a great way to get bitten on the butt later on.


More information about the devel mailing list