0

[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
2
  • By the way, I can find another solution. I need to get only digits from $TAGS. I used echo $TAGS | sed -e 's/[^0-9]//g' and it works! Commented Mar 8, 2015 at 10:55
  • Yes, that's a workaround, but it won't work in all situations. Specifically, you won't be able to emulate =~'s support for capture groups. Commented Mar 8, 2015 at 17:50

2 Answers 2

4

There are several problems:

  1. bash doesn't support \d. Use [0-9].
  2. Whitespace is needed around the operator: $TAGS =~ $re, otherwise bash parses it as [[ -n "$TAGS=~$re" ]].
  3. Path to the shell is /bin/bash, not bin/bash.
Sign up to request clarification or add additional context in comments.

7 Comments

About whitespaces. If I wrote $TAGS =~ $re I have an error conditional binary operator expected syntax error near =~
@MaximBanaev: Strange. I get a similar error if I put whitespace at one side of the =~ only. Maybe old bash version?
Maybe I have misses something here, but it looks like you are declaring a variable TAGS="MASTER_46" - and referring to is as an array. Is that possible in Bash?
@asimovwasright: ${var} is the same as $var, but different to ${var[@]}.
The first capturing parenthese is [1] -- the whole matched text (including the leading underscore) is in [0]
|
2

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.

3 Comments

I am using msysgit for windows (version 1.9.5). Maybe I need to install some libs. Anyway echo $BASH_VERSION returns 3.1.20(4)-release
I think here answer for my questin
@MaximBanaev: Good find. I've updated my answer accordingly. The short of it: Install MSYS instead, and simply reference git.exe from the msysgit installation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.