Search code examples
regexbashshellmsysgit

How can I capture group from string using regexp in shell in msysgit on Windows?


[Editor's note: The OP has later clarified that he's running bash as part of msysgit, the Git version for Windows.]

I'm trying to get last digits from the string. I have a little script but it doesn't work and i don't know why:

 #!bin/bash
 TAGS="MASTER_46"
 re="_(\d+)"
 if [[ ${TAGS}=~$re ]]; then 
     echo "Find"
     echo ${BASH_REMATCH} 
     echo ${BASH_REMATCH[1]} 
 fi

The output:

Find
{empty}
{empty}

I am using bash

$ bash --version
  GNU bash, version 3.1.20(4)-release

Solution

  • Update, based on the OP's clarification re environment and his own findings:

    tl;dr:

    • msysgit (as of version 1.9.5) comes with a bash executable that is compiled without support for =~, the regex-matching operator
    • A limited workaround is to use utilities such as grep, sed, and awk instead.
    • To solve this problem fundamentally, install a separate Unix emulation environment such as MSYS or Cygwin, and use git.exe (the core of msysgit) from there.

    choroba's answer has great pointers, but let me add that, since you get the following error message:

    conditional binary operator expected syntax error near =~

    the implication is either that

    • your bash version is too old to support =~, the regex-matching operator.
    • your bash version was compiled without regex support

    Given that =~ was introduced in bash 3.0 (see http://tiswww.case.edu/php/chet/bash/NEWS) and you're running 3.1.x, it must be the latter, which indeed turned out to be true:

    The OP runs msysgit, the official Git version for Windows that comes with bash and a set of Unix utilities.

    As it turns out, as of version 1.9.5, the bash executable that comes with msysgit is built without regex support, presumably due to technical difficulties - see https://groups.google.com/forum/#!topic/msysgit/yPh85MPDyfE.

    Incredibly, the "Known Issues" section of the release notes does not mention this limitation.

    Your best bet is to:

    • Install MSYS to use as your Unix emulation environment - its bash does come with =~ support (note that msysgit was originally forked from MSYS).
      • Alternatively, to get better Unix emulation and more tools at the expense of a larger installation footprint and possibly performance, install Cygwin instead.
    • In MSYS, use only git.exe from msysgit, via the Windows %PATH%.
      • To that end, be sure to install msysgit with the Run Git from the Windows Command Prompt option - see https://stackoverflow.com/a/5890222/45375
      • Alternatively, add C:\Program Files\Git\cmd (assumes the default path on 32-bit Windows, on 64-bit Windows it's C:\Program Files (x86)\Git\cmd) manually to your Windows %PATH%, OR extend $PATH in your MSYS ~/.profile file (e.g., PATH="$PATH:/c/program files/git/cmd").

    You could hack your msysgit installation, but it hardly seems worth it and I don't know what the side effects are; If you really wanted to try, do the following: Copy the following files from an MSYS installation's bin directory to msysgit's bin directory: bash.exe, sh.exe, msys-termcap-0.dll - in other words: replace msysgit's bash altogether.