Using Bash scripts from Windows

For certain use cases, it’s feasible to run a Bash script from within Windows. The obvious ways to do this are via Cygwin and Windows Subsystem for Linux. However, there is a third way to run Bash scripts from within Windows itself, not needing Cygwin or Windows Subsystem for Linux. In this example, we rely on the Bash shell installed with Git on Windows.

The trick is to start the Bash script you want to run from Linux or Windows with the shebang (first line of Bash script file):

#!/bin/bash; C:/Program\ Files/Git/user/bin/sh.exe

This tells the shell (Linux or Windows) to first try /bin/bash which is a Unix-like path, and then try the Git Bash shell on Windows. If Python is on your Windows Path, you can even use Bash scripts that also invoke Python scripts. This is very handy in general.