pipe is returning empty string in bash in git for windows

后端 未结 2 995
时光取名叫无心
时光取名叫无心 2021-02-14 09:16

Edit: Issue has been resolved after update to Git for Windows >= 2.9.0.windows1


Disclaimer

Some comments are referring to full \"story\" behind this is

相关标签:
2条回答
  • 2021-02-14 09:21
    basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
    

    This seems like an example of the XY problem. Let us break down this line:

    echo "$0"
    

    This is usually the path to the script, for example ./alfa.sh

    sed -e 's,\\,/,g'
    

    This replaces backslashes with forward slashes. This is where this line starts to fall apart:

    1. You dont need the -e, you can just do sed 's,\\,/,g'

    2. You probably dont need the g, usually just going to be one slash as shown above

    3. Changing the slashes doesnt really make sense. Bash, even on Windows, is going to be using forward slashes already

    4. If for some reason the slashes do need to be changed, Sed is not the right tool for this anyway, cygpath is:

      $ cygpath -m 'C:\Program Files\Mozilla Firefox\firefox.exe'
      C:/Program Files/Mozilla Firefox/firefox.exe
      
    dirname
    

    Now you are calling dirname after sed/cygpath. It should be called before, that way sed/cygpath dont have to replace as much:

    basedir=$(cygpath -m "$(dirname "$0")")
    

    Finally, the sed command is bad for another reason; if you are going to be spitting out a path, it should be an absolute one, because why not?

    basedir=$(cygpath -am "$(dirname "$0")")
    

    Notice now that no pipe is even involved. I will also add that this problem was introduced recently to the NPM repo. You might comment to the devs there.

    0 讨论(0)
  • 2021-02-14 09:24

    Summarizing the comments, the short (tl;dr) version: either downgrade, upgrade, and/or re-install MSYS and MinGW that come with Git for Windows.

    MSYS supplements MinGW, and the version provided by Git for Windows may be modified from the original maintainers of MSYS. There was a bug reported against MSYS for what appears to be this same issue (using "mingw version: 64 bit bundled with git version 2.8.3.windows.1"), but was marked as "works for me" (i.e., "can't reproduce"). But there was a comment that the problem could be in the repackaging:

    "Please be advised that MSYS, as bundled with git for windows, may be modified from our official distribution, (and 64-bit MinGW certainly isn't ours); thus we don't formally support either of these." https://sourceforge.net/p/mingw/bugs/2303/

    Long story short, looks like a bug.

    0 讨论(0)
提交回复
热议问题