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

Arca Artem picture Arca Artem · Jan 26, 2014 · Viewed 10.6k times · Source

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 temporary command-prompt session which itself running under a bash session.

My success rate is 50/50 as some command works as expected whereas others not so much.

I think the problem may lie around arguments not lining up properly (i.e. missing or merged arguments)

I'll try to explain what I mean by weird by a series of commands and responses. (I'm trying to get the word test to be printed on the screen.)

I'm running these under GNU bash, version 3.1.0(1)-release (i686-pc-msys) Bundled with Git-1.8.4:

First attempt:

$ cmd /c echo test
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
c:\>

Second attempt:

$ cmd '/c echo test'
test"

Third attempt:

$ cmd "/c echo test"
test"

Fourth attempt:

$ cmd /c\ echo\ test
test"

Fifth attempt:

$ cmd "/c echo" test
'echo" test' is not recognized as an internal or external command,
operable program or batch file.

I'd really appreciate any pointers or insights into the behaviors above as this is unintuitional to me and driving me crazy!

Edit: There is another question that appears similar to this, but it really isn't, mainly because it's about running batch files through CMD /C that doesn't require any arguments.

It doesn't really answer my question about how to provide arguments properly to windows command line apps, and even though the examples are about CMD /C, the answer here can be applied to many other Windows command line apps as well.

Answer

patthoyts picture patthoyts · Feb 20, 2014

This is actually documented in the ReleaseNotes file (in the top level folder of your installed Git for Windows)

Also, extra care has to be paid to pass Windows programs Windows paths, as they have no clue about MSys style POSIX paths -- You can use something like $(cmd //c echo "$POSIXPATH").

If you use cmd //c echo test it works as expected.

$ cmd //c echo test
test

The cause is to do with trying to ensure that posix paths end up being passed to the git utilities properly. For this reason, Git for Windows includes a modified MSYS layer that affects command arguments. You should note that it is not intended that the bash shell and tools provided with Git for Windows be used as general purpose unix tools for Windows. If you want a general purpose unix-style toolset then you should install MSYS or cygwin. The Git Bash shell is setup for working with git and sometimes that shows.