Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error =~ operator in msysgit bash

I'm trying to add a function to my bash_profile for msysgit:

function git-unpushed {
    brinfo=$(git branch -v | grep git-branch-name)
    if [[ $brinfo =~ ("[ahead "([[:digit:]]*)]) ]]
    then
        echo "(${BASH_REMATCH[2]})"
    fi
}

But I get the following error:

bash: conditional binary operator expected`

bash: syntax error near =~'

From what I can find, the "equals tilde" operator (=~) evaluates as regex in bash.

Why is =~ is throwing an error?

UPDATE: Here's a screenshot of inputting it manually (this is running sh.exe):

Screenshot of equals tilde (=~) operator failing

like image 788
Aaron Blenkush Avatar asked Mar 19 '13 20:03

Aaron Blenkush


People also ask

How do I fix syntax errors in a bash script?

Run the script that contains the syntax error. Take note of the line mentioned by the Bash error. Execute the line with the error in a Bash shell to find the error fast (without having to change the script and rerun it multiple times). Update your script with the correct line of code. Confirm the script works.

What can go wrong in a bash script?

There are many things that can go wrong in a Bash script and cause this error. Some common causes are missing spaces next to commands and lack of escaping for characters that have a special meaning for the Bash shell.

What does the syntax error in/bin/sh mean?

On Ubuntu, /bin/sh is dash, a shell designed for fast startup and execution with only standard features. When dash reaches line 68, it sees a syntax error: that parenthesis doesn't mean anything to it in context. Since dash (like all other shells) is an interpreter, it won't complain until the execution reaches the problematic line.

What does ‘then ‘ mean in a syntax error?

There is a syntax error. The token ‘ then ‘ is unexpected. All good! Lesson 3: When you see a syntax error verify that you are using Bash loops or conditional constructs in the right way and you are not adding any statements that shouldn’t be there.


3 Answers

I had the same error on Bash 3.1.0 from Git installation on Windows. Ultimately I changed it to:

if echo $var | grep -E 'regexp' > /dev/null
then
  ...
fi
like image 197
Lukasz Korzybski Avatar answered Oct 24 '22 09:10

Lukasz Korzybski


According to https://groups.google.com/forum/#!topic/msysgit/yPh85MPDyfE this is because msys doesn't ship libregex along with bash. Supposedly if you compile/find an msys built libregex, and put it in the library path, =~ starts working fine.

like image 24
Eris Avatar answered Oct 24 '22 09:10

Eris


Update 2015: msysgit is now obsolete.
You should use the bash which comes with git-for-windows.
As mentioned in this answer, it uses a much more recent bash (4.3+), for which the =~ syntax will work.


Original answer (march 2013)

The bash packaged with msysgit might simply be too old to fully support this operator.
It is certainly too old to compare with unquoted regex, as mentioned in "Bash, version 3" and "How do I use regular expressions in bash scripts?":

As of version 3.2 of Bash, expression to match no longer quoted.

Actually, mklement0 mentions in the comments:

=~ was introduced in bash 3.0 and always supported an unquoted token on the RHS.
Up to 3.1.x, quoted tokens were treated the same as unquoted tokens: both were interpreted as regexes.
What changed in 3.2 was that quoted tokens (or quoted substrings of a token) are now treated as literals.

But I tried with quotes (in the latest msysgit 1.8.1.2), and it still fails:

vonc@voncvb /
$ /bin/bash --version
GNU bash, version 3.1.0(1)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.
vonc@voncvb /
$ variable="This is a fine mess."
vonc@voncvb /
$ echo "$variable"
This is a fine mess.
vonc@voncvb /
$ if [[ "$variable" =~ T.........fin*es* ]] ; then echo "ok" ; fi
bash: conditional binary operator expected
bash: syntax error near `=~'
vonc@voncvb /
$ if [[ "$variable" =~ "T.........fin*es*" ]] ; then echo "ok" ; fi
bash: conditional binary operator expected
bash: syntax error near `=~'
vonc@voncvb /
like image 7
VonC Avatar answered Oct 24 '22 09:10

VonC