77

I have a small utility script called clear.bat that does some housekeeping work on my sources.

It is a .bat file so that I could easily double-click it in Windows Explorer.

Sometimes, I find it more handy to execute it from my Git bash (msysgit, if this matters).

To do this, I type

cmd
clear.bat
exit

cmd turns my Git bash into a normal cmd window where I could easily execute my batch. When I type exit, the cmd environment is terminated and I'm back in my Git bash.

Could this be achieved in an easier way?

I tried cmd /C clean.bat since the docs say

Syntax
      CMD [charset] [options]

      CMD [charset] [options] [/c Command] 

      CMD [charset] [options] [/k Command] 

Options   
   /C     Run Command and then terminate

   /K     Run Command and then return to the CMD prompt.
          This is useful for testing, to examine variables

Edit:
Just noticed that the post is broken.

What I want is to execute clean.bat from within the Git bash without having to type the three commands above (cmd, clear.bat, exit). I just want to execute the .bat file from within my Git bash. Obvious way would be to create a separate .sh file that does the same work but this will lead to double code.


Edit 2: When I execute cmd /C clean.bat, the Git bash turns into a plain CMD environment and only displays the prompt. The file clean.bat does not get executed. It's the same as if I just type cmd.

Also, adding a /debug switch does literally nothing. Seems like only cmd gets evaluated and all further parameters are getting ignored.

eckes
  • 56,506
  • 25
  • 151
  • 189
  • OK, I'm not sure I understand the problem, then. – CB Bailey Aug 08 '12 at 14:01
  • What was the output you got when you did `cmd /C clean.bat` ? You might want to try throwing some quotes around the string, or the `/K` option instead of `/C`, or adding `/debug` (not sure if debug will help, but it sounds goods). – Sunil D. Aug 08 '12 at 17:44
  • 7
    More correct explanation is here: [Weirdness when running CMD.exe /C under Git bash](http://stackoverflow.com/q/21357813/367456) – hakre Mar 15 '14 at 14:17
  • 5
    `cmd /C` doesn't work in Git Bash because it thinks `/C` is a path and turns it into `C:\`. Use `cmd //C` instead. See @hakre's link for more details. – Joey Adams Nov 10 '14 at 15:36
  • Possible duplicate of [Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args](https://stackoverflow.com/questions/21357813/bash-in-git-for-windows-weirdness-when-running-a-command-with-cmd-exe-c-with-a) – Aserre Jun 11 '19 at 10:41
  • It was very nice too know that cmd convert git bash to cmd! – Meysam Sadeghi Feb 17 '21 at 07:27

5 Answers5

97

After playing around a bit more, I found the solution myself:

cmd "/C clean.bat"

does the trick. But I got no clue, why...

eckes
  • 56,506
  • 25
  • 151
  • 189
  • 4
    Yes, I also wanted to post this for you - the reason might be here (it is a known issue): http://code.google.com/p/msysgit/issues/detail?id=426 – rlegendi Aug 09 '12 at 08:18
  • 2
    Is there any way to send parameters as well? – dr3w Oct 29 '13 at 11:15
  • @dr3w: shall be as easy as `cmd "/C clean.bat param1 param2"` – eckes Oct 29 '13 at 11:47
  • 1
    @eckes nope, not working for me. I'm trying to execute cmd `"/C nvmw help"` and get an error: `The syntax of the command is incorrect.`, while simply `cmd "/C nvmw"` works – dr3w Oct 30 '13 at 13:49
  • @dr3w: maybe cmd "/C nvmw" help – eckes Oct 31 '13 at 08:29
  • 4
    I wrote a git bash script for this called "bat", now you can do bat mybat.bat on command line and it will execute #!/bin/bash cmd "/C $1" – relipse Apr 05 '14 at 07:04
  • If you want to run something from a different path, you'll have to escape the separators, e.g. `cmd "/C \\\\SERVERNAME\\folder\\folder\\myscript.cmd"` – Philipp Sumi May 16 '14 at 15:54
  • 7
    The following solves the arguments issue. Spoiler: `//c`. http://stackoverflow.com/a/21907301/435605 – AlikElzin-kilaka Jul 30 '15 at 10:51
  • You can also just hit a space behind the last argument. Example: `cmd "/C dosome.bat hello "` will execute **dosome.bat** with argument **hello**. – MC Emperor Jun 15 '17 at 12:06
  • From the documentation you posted I would have guessed `cmd /C /c clear.bat` but apparently Windows doesn't work like that – user1129682 Nov 08 '18 at 08:10
  • @rlegendi the code.google.com link you posted above is no longer valid, and it's not on the Wayback Machine either. – AJM-Reinstate-Monica May 14 '21 at 18:10
  • Someone else posted with the same issue at https://github.com/msysgit/msysgit/issues/101. The reply from devs was "Long story short, this is an upstream MSYS issue, and there's also already a feature request filed for it at https://sourceforge.net/p/mingw/bugs/1902/ " – AJM-Reinstate-Monica May 14 '21 at 18:12
49

./clear.bat will do the trick.

Hauleth
  • 20,457
  • 4
  • 58
  • 98
half-potato
  • 819
  • 1
  • 9
  • 15
  • 1
    in the old (but company-bundled) msysgit 1.8.0, this exectues the .bat as a bash script – bernard paulus Aug 07 '17 at 09:08
  • Maybe add that this is new functionality which was not available at the time of the question? – kalj Feb 01 '18 at 13:43
  • 2
    This seems to work, but how does Git bash know that this is a batch file? – HelloGoodbye Dec 14 '18 at 11:26
  • 1
    Typically the shebang line (ie #!/bin/bash) would indicate what bash should use to execute the file. However, on windows the default behavior without a shebang is to treat the file like bash file. – half-potato May 21 '19 at 18:07
  • Note that there is a limitation to this; if your file has more than one extension - _e.g._ ```some-file.extension.cmd``` - you may end up getting ```Permission denied```. For this cases you need to do ```cmd "/c some-file.extension.cmd"```. – Marco Craveiro Mar 16 '21 at 10:31
6

The Git for Windows (msysGit has been superseded by Git for Windows1) FAQ says you have 3 options:

  • Run programs that have problems using the winpty utility. This allows you to keep using the nicer mintty terminal, but can become unwieldy if you need the workaround for many programs.

  • Modify the shortcut for Git Bash to run bash directly without mintty so it uses the default console host and configure it for "Quick Edit", reasonable size and scroll-back and suitable unicode font. You'll still have to live with the other quirks of console host.

  • Install and use ConEmu.

  • 1
    Nope. The FAQ says **Some native console programs don't work when run from Git Bash. How to fix it?**. This means that for example some programs might crash when you run them out of the Git bash. But that is not the issue being discussed above. Actually, the issue still exists (tried with 2.6.1.windows.1 (latest and greatest)), even when using ConEmu! – eckes Oct 16 '15 at 12:27
  • Did you try `winpy`? It worked for me. Just run `winpty clean.bat` – Yitzhak Steinmetz Oct 16 '15 at 12:34
  • 2
    No I did not. However, since this behavior of the bash is known, it's not surprising that winpty does -- beside doing it's original job -- also fix this issue. But the correct solution is to use `cmd //c` (http://stackoverflow.com/questions/21357813/weirdness-when-running-cmd-exe-c-under-git-bash) and not installing another software! – eckes Oct 16 '15 at 12:41
  • 1
    `winpty` comes out of the box with git bash. I'd try it before ruling it out. – Yitzhak Steinmetz Oct 16 '15 at 12:43
  • ok. Comes out of the box. But still is another program being called instead of simply using the `cmd //c` syntax. Again: `winpty` does accidently solve the issue but the other two options don't help at all. – eckes Oct 16 '15 at 12:59
  • 1
    `cmd` is also another program, and the syntax `cmd //c clean.bat` is definitely more cumbersome than `winpty clean.bat`. I quoted the other two option for the general purpose answer, even though they might not apply to your specific problem. – Yitzhak Steinmetz Oct 17 '15 at 17:41
  • I mean `cmd "//c clean.bat"`, which is even more cumbersome. – Yitzhak Steinmetz Oct 17 '15 at 17:48
2

I like start clean, it opens a new window with cmd. This method has some benefits:

  • cmd.exe gets a native console
  • the new console has a native windows character encoding (e.g. cp1251 vs utf8)
lav
  • 1,177
  • 8
  • 16
2

At some point, Git for windows added support for the MSYS_NO_PATHCONV environment variable, so in addition to @eckes and @AlikElzin-kilaka solutions, you can also

MSYS_NO_PATHCONV=1 cmd /c clean.bat

In general, I prefer this solution, as it allows the code to be the closest to resembling normal bash, and there are many ways to export MSYS_NO_PATHCONV depending on your preferred situation.

Note: Git for Window's bash does not support the MSYS2 environment variable MSYS2_ARG_CONV_EXCL

The other solutions

The weird quoting solution

Why does cmd "/c clean.bat" not create other errors?

It turns out argument parsing in windows does not follow the same universal rules as it does in *nix. Instead, in windows the arguments are parsed differently based on the runtime you compile against. Basically in windows, the command line arguments are passed in as "one string" and then parsed by the runtime.

See here for more explanation than you could ever want.

E.g. cmd parses arguments differently then wscript.exe

In the end, you can hopefully find something that works with this method, and it is the most "window-esque" of the three solutions

The // method

This is pretty well explained here and simple to use, but adds an extra / which does not help readability

Andy
  • 2,207
  • 1
  • 12
  • 22