Syntax error =~ operator in msysgit bash

后端 未结 4 1902
迷失自我
迷失自我 2020-12-18 19:40

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          


        
4条回答
  •  清酒与你
    2020-12-18 20:10

    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 /
    

提交回复
热议问题