Associate SH scripts to Windows bash

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


The equivalent Vbscript method (for PSSGCSim's C# code) would be:

If WScript.arguments.count <> 0 And LCase(Right(WScript.Arguments(0), 3)) = ".sh" Then
    Dim WshShell: Set WshShell = WScript.CreateObject("Wscript.Shell")
    strSHfile = WScript.Arguments(0)
    MyArr = Split(strSHfile, ":")
    strSHfile = "/mnt/" & LCase(MyArr(0)) & MyArr(1)
    strSHfile = Replace(strSHfile,"\","/")
    WshShell.Run "%systemroot%\system32\bash.exe " & """" & sSHfile  & """",,True
    Set WshShell = Nothing
End If

And the file association REG file is here:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.sh]
@="shfile"

[HKEY_CLASSES_ROOT\shfile]
@="SH Script File"

[HKEY_CLASSES_ROOT\shfile\shell\open\command]
@="wscript.exe \"D:\\Scripts\\bash.vbs\" \"%1\""

To make it run .SH files from network, you need to alter the script.