Windows 10 – How to Associate SH Scripts with Windows Bash

bashwindows 10windows-10-v1607windows-subsystem-for-linux

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.

Best Answer

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)