[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
Update, based on the OP's clarification re environment and his own findings:
tl;dr:
=~
, the regex-matching operatorgrep
, sed
, and awk
instead.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
=~
, the regex-matching operator.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:
=~
support (note that msysgit was originally forked from MSYS).
git.exe
from msysgit, via the Windows %PATH%
.
Run Git from the Windows Command Prompt
option - see https://stackoverflow.com/a/5890222/45375C:\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.