Associate .SH Shell Scripts with Bash in Windows 10

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


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

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.

    Reply

Leave a Comment