19

When I execute a .bat script from bash in Cygwin, by what mechanism is it running? I understand that if I run a .EXE it will launch, regardless of whether the .EXE is from Cygwin or from a more traditional environment. I understand that when I execute an executable script with #! at the beginning that Cygwin supplies the magic for it to run.

But why does a .bat script work? Is there some component inside of Cygwin that is aware of what a Windows .bat script is and what to do with it? Or is it that it is somehow impossible under Windows to execute a call to launch a .EXE file that won't automatically also work for a .bat script instead?

skiphoppy
  • 97,646
  • 72
  • 174
  • 218

3 Answers3

13

Running

./test.bat params

from bash seems to be equivalent to

cmd /c test.bat params
Tunzi
  • 131
  • 1
  • 2
12

I believe that bash in cygwin sees the bat extension as being flagged executable (a cygwin hit-tip to windows convention). As such it loads and executes the file with it's associated interpreter (cmd.exe, per os configuration), much as it creates a new instance of bash to run your #! scripts (per posix standard).

BnWasteland
  • 2,109
  • 1
  • 18
  • 14
  • 2
    For normal UNIX shell scripts it should load the program that is specified after the shebang, not blindly use bash (and scripts that specify bash there are likely to be not very portable :)) – Joey Apr 25 '09 at 05:28
  • 4
    This works for .bat but not for .ps1 (PowerShell). Cygwin must have some built-in handling of .bat files. – Bogdan Calmac Oct 31 '13 at 01:52
1

And if you want to fork an *.cmd file execution like a ShellScript process and append his log to an file:

cmd /c test.bat > nohup.out &

Enjoy!

Mr. Anderson
  • 1,465
  • 1
  • 14
  • 15