Associate .SH Shell Scripts with Bash in Windows 10

0 Shares

If you’ve installed Bash on Ubuntu on Windows in Windows 10 version 1607 and higher, and want to associate .SH shell scripts with Bash, here is a quick script solution.

To access Windows file system path in Bash, you use the following syntax or semantics:

/mnt/c/Windows/MyTasks.sh

I wrote a small Vbscript that converts the Windows file path such as C:\Windows\MyTasks.sh, to *NIX file system path. The script then launches Bash passing the *NIX file path as the argument. This script can be associated with .SH file types using the registry edit supplied herewith.

Download

Download bash_sh_assoc.zip, unzip and run the enclosed REG file. Then move the script file bash.vbs to the Windows directory. This associates .SH file types with bash.vbs script file.

Contents of the Vbscript file Bash.vbs

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

Contents of the REG file

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.sh]
@="shfile"

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

[HKEY_CLASSES_ROOT\shfile\defaulticon]
@="%USERPROFILE%\\AppData\\Local\\lxss\\bash.ico"

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

Related post

Add Bash to the Right-Click Menu for Folders in Windows 10

0 Shares

About the author

Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and has a vast experience in the ITeS industry — delivering support for Microsoft's consumer products. He has been a Microsoft MVP [2003 to 2012] who contributes to various Windows support forums.

2 thoughts on “Associate .SH Shell Scripts with Bash in Windows 10”

  1. ERROR:

    The .reg file in the bash_sh_assoc.zip download does not match the contents shown above and will not work. Looks like it was the author’s development copy.

    The vbs file only differs in the use of %systemroot% instead of %windir%, which makes difference.

Leave a Comment