Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

windows - Syntax error =~ operator in msysgit bash

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...