How can I associate all .sh files to new BASH included in Windows 10 Anniversary update?

I have tried associating .sh file using default system prompt to C:\Windows\System32\bash.exe but it just flashes console window with no result.


It's also possible to do with just a batch file:

@echo off

REM Get the full qualified path for the first argument
SET fullpath=%~f1

REM Get the drive letter and convert it to lowercase
SET drive=%fullpath:~0,1%
FOR %%i IN ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO CALL SET drive=%%drive:%%~i%%

REM Replace \ with /
SET relpath=%fullpath:~3%
SET relpath=%relpath:\=/%

C:\Windows\System32\bash.exe --login "/mnt/%drive%/%relpath%"

Now just use Open with... (and remember to check Always use this app to open .sh files) to associate the sh files with this batch file.

EDIT: included --login argument for bash.exe to set all the appropriate linux specific environment variables (such as $PATH)


The problem is that BASH uses Unix-like paths and Windows gives DOS paths. So you need to redirect DOS path to Unix path.

My solution

It is more like hack than a real solution. USE AT YOUR OWN RISK

Write tiny C# console app to redirect paths

string[] splt = args[0].Split(':');
string exe = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\bash.exe";
string arguments = "/mnt/" + splt[0].ToLower() + splt[1].Replace('\\', '/');

using (Process process = new Process())
{
    process.StartInfo.FileName = exe;
    process.StartInfo.Arguments = arguments;

    process.Start();
    process.WaitForExit();

    return process.ExitCode;
}

And then you associate .sh files with this C# app

Dirty but works