Bash: = ~ and pattern matching in Git Bash 3.1
Basically I 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
=~
doesn't work because it doesn't exist in this version of Bash.
Note. The script works with Bash 4.3.33 on Gentoo.
Git and Bash version:
$ 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 only Bash 3.1 documentation I could find, the version is:
This is version 3.1-beta1, last updated on September 5, 2005, GNU Bash Reference Manual, for Bash, Version 3.1-beta1 .
This documentation states =~
:
An additional binary operator is available, '= ~', with the same precedence as' == 'and'! = '.
Am I missing something like some kind of option using shopt?
source to share
You can just use a newer version of this bash: use git-for-windows , which will soon replace the deprecated msysgit.
No installation required: unzip PortableGit-2.4.6-5th-release-candidate-64-bit.7z.exe
anywhere and call c:\path\to\PortableGit-2.4.6-5th-release-candidate-64-bit\git-bash.exe
.
You will get 2013 4.3.39 bash (instead of the old 2005 3.1.20 bash from msysgit): this is one of the most recent 4.3 patches , May 2015.
This is more than enough for the job =~
.
source to share