Bash: =~ 和模式匹配 Git Bash 3.1

Bash: =~ and pattern matching in Git Bash 3.1

我基本上想在 Bash 3.1 下 Windows (Git Bash) 中执行以下操作:

#!/bin/bash
#set -x
shopt -s extglob
shopt -s nocasematch

declare file='[Vol 01] - 04 - This message'
declare filesafe="${file}"

declare pattern='\[Vol ([0-9]+)\] - ([0-9]+) -*'
if [[ "${file}" =~ $pattern ]]; then
  echo "regexp: $(printf "%s %-2d %-3d" "${filesafe}" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" ) - - -"
else
        echo "failed: $(printf "%s" "${filesafe}") - - - - -"
fi

=~ 不起作用,因为它不存在于此版本的 Bash。

注意:该脚本适用于 Gentoo 中的 Bash 4.3.33。

Git & Bash 版本如下:

$ git --version ; bash --version
git version 1.9.5.msysgit.1
GNU bash, version 3.1.23(6)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.

在我能找到的关于 Bash 3.1 的唯一文档中,版本是:

This is Edition 3.1-beta1, last updated 5 September 2005, of The GNU Bash Reference Manual, for Bash, Version 3.1-beta1.

此文档说明 =~ 可用:

An additional binary operator, '=~', is available, with the same precedence as '==' and '!='.

我是否错过了一些使用 shopt 的选项?

您可以简单地使用该 bash 的更新版本:使用 git-for-windows,它将很快取代过时的 msysgit。

无需设置:将 PortableGit-2.4.6-5th-release-candidate-64-bit.7z.exe 解压缩到任意位置,然后调用 c:\path\to\PortableGit-2.4.6-5th-release-candidate-64-bit\git-bash.exe

You will get a 2013 4.3.39 bash (instead of the old 2005 3.1.20 bash of msysgit): that is one of the very latest 4.3 patches,2015 年 5 月。

这足以让 =~ 工作。