3

I basically want to do the following in Bash 3.1 under 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

The =~ does not work because it does not exists in this version of Bash.

Note: the script works with Bash 4.3.33 in Gentoo.

Version of Git & Bash are the following:

$ 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.

In the sole documentation on Bash 3.1 that I could find, the version is:

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

This documentation state the =~ is available:

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

Did I miss something like some option using shopt?

NoDataFound
  • 11,381
  • 33
  • 59
  • just a side note, don't include your pattern inside quotes. – Avinash Raj Jul 18 '15 at 15:03
  • @anubhava, ...should, in theory, but the msys build of bash shipped by that vintage of Git for Windows is missing other features available in upstream as well -- try using process substitution, f'rinstance. – Charles Duffy Jul 19 '15 at 15:32
  • File is some file like {{[Vol 02] - 0001 - Volume 2 chapter 1}}. However, my question lies more in the implementation of =~ operator in Git Bash under Windows, the code is just an example where it fails rather explicitly (like "I don't recognize =~ dude!"). – NoDataFound Jul 19 '15 at 15:37

1 Answers1

3

You can simply use a more recent version of that bash: use git-for-windows, which will soon replace the obsolete msysgit.

No setup required: uncompress PortableGit-2.4.6-5th-release-candidate-64-bit.7z.exe anywhere you want, and call 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, May 2015.

That is more than enough to make =~ work.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Is there a list of supported version of Windows ? I'm stuck with XP at work, and while the script (and my question) is for something at home, I'm really interested to switch to git-for-windows now. – NoDataFound Jul 19 '15 at 15:59
  • @NoDataFound Xp works just fine. Just uncompress the 32-bits version. https://github.com/git-for-windows/git/releases/download/v2.4.6.windows.1/PortableGit-2.4.6-5th-release-candidate-32-bit.7z.exe – VonC Jul 19 '15 at 16:00