Out of a git console: how do I execute a batch file and then return to git console?

Viewed 80506

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.

6 Answers

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

This will work and it frees the terminal too

  nohup ./nucleus.bat &
  less nohup.out
Related