Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args

前端 未结 7 2264
后悔当初
后悔当初 2020-12-05 02:55

This is more of an annoyance rather than a problem but I would very much like to understand the semantics here.

All I want to do is to run an arbitrary command on a

7条回答
  •  我在风中等你
    2020-12-05 03:06

    I am able to mostly reproduce the problem using gnu bash for Windows.

    I can't quite establish a pattern with the first form without any quotes. It seems to work with the Windows ECHO command, but not with other commands like DIR. EDIT - It turns out gnu bash is putting quotes around my command, so echo test becomes "echo" "test". The quotes cause cmd.exe to look for an external command instead of the internal ECHO command. I happen to have "echo.exe", so it appears to run. The odd thing is the quotes around test are not displayed. When I attempt to run the DIR command, it fails entirely because there isn't any DIR.EXE.

    The subsequent forms with quotes (except the last one), or escaped spaces, work the same as you are seeing - there is an unwanted trailing quote in the command.

    I could not come up with a clean solution. However, I have an ugly hack that should give you the desired result. Simply concatenate a REM command at the end of your command. The REM will comment out the unwanted trailing quote. It is important that there be a space after REM, otherwise REM" will not be recognized as a valid command. Any of the following should work.

    $ cmd '/c echo test&rem '
    $ cmd "/c echo test&rem "
    $ cmd /c\ echo\ test\&rem\ 
    

    Note that the last command has a space after the backslash.

    The technique should work for pretty much any command string that you might want to execute via CMD.EXE.

提交回复
热议问题