in f13 =~ no longer working in bash

James Findley sixy at gmx.com
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:
> http://git.annexia.org/?p=libguestfs.git;a=commitdiff;h=457fccae1b665347f81045e8c7a3309d8328c4fc
>
> and out later, complete fix:
> http://git.annexia.org/?p=libguestfs.git;a=commitdiff;h=4891ff9945177e8666af8381d1e0a54b8ce363e2
>
> 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.

-siXy


More information about the devel mailing list