Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 247
Arca Artem Avatar asked Jan 25 '14 23:01

Arca Artem


People also ask

Can I use Git bash instead of cmd in Windows?

You don't need to use Git Bash. It is just conventional, because Git was developed and designed on Linux and most Git users use Linux and using Git Bash on Windows makes it a uniform experience. You can certainly use Git on cmd; just make sure you add C:\Program Files\git\cmd to your PATH .

How do I run Git bash from Windows command line?

Launch Git Bash console by clicking on the Start button, type git, and click on Git Bash. 2. Run the below git config command to add your name ( YourName ) as your git username ( user.name ). The git config command administers configuration variables that control how Git looks and operates.

Is Git bash better than cmd?

It is up to you to decide which you want to use. As many others, I recommend you to learn Git with command line before switching to a graphical interface. If you don't know which to choose between Git Bash and Git CMD, I'd go for Git Bash since bash is a really useful tool to learn.

Does cmd support bash?

You can use the built-in Command Prompt, PowerShell, or Bourn shell itself. Let's look at all three. Click Start, All Apps, under the letter B click Bash on Ubuntu for Windows. Press Windows key + X then click Command prompt, at the command prompt, type: bash then hit Enter.


2 Answers

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.

like image 91
patthoyts Avatar answered Sep 20 '22 00:09

patthoyts


After reading this article I've found solution which works for me:

$ cat gvim.sh cmd << EOD gvim $@ EOD $ 

Windows 8.1, Git (version 1.9.5-preview20141217), GNU bash, version 3.1.20(4)-release (i686-pc-msys).

like image 36
avb1003 Avatar answered Sep 18 '22 00:09

avb1003