Skip to content

Instantly share code, notes, and snippets.

@stefansundin
Last active February 2, 2021 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefansundin/83933f6e71bc81aa66e6d2bf0a5602a3 to your computer and use it in GitHub Desktop.
Save stefansundin/83933f6e71bc81aa66e6d2bf0a5602a3 to your computer and use it in GitHub Desktop.
Bash on Windows shell script file association
#!/bin/bash
# Put this file in C:\ and then run z-register.bat by right clicking it and using "Run as Administrator"
# Translate the windows path to the equivalent Bash on Windows path, then run it!
drive="${1:0:1}"
path="${1:2}"
dir=$(dirname "/mnt/${drive,,}${path//\\/\/}")
cd "$dir"
"/mnt/${drive,,}${path//\\/\/}"
echo
read -p "Press enter key to close window..."
@echo off
@echo.
echo If you see "ERROR: Access is denied." then you need to right click and use "Run as Administrator".
@echo.
echo Removing .sh association...
reg delete HKCR\.sh /f
reg delete HKCR\sh_sh /f
@echo.
pause
@echo off
@echo.
if not exist "C:\sh.sh" (
echo Warning: Can't find C:\sh.sh
echo Please move sh.sh there.
@echo.
pause
exit /b
)
echo If you see "ERROR: Access is denied." then you need to right click and use "Run as Administrator".
@echo.
echo Associating .sh
reg add HKCR\.sh /ve /t REG_SZ /d "sh_sh" /f
reg add HKCR\sh_sh /ve /t REG_SZ /d "Shell Script" /f
reg add HKCR\sh_sh\DefaultIcon /ve /t REG_SZ /d "%%USERPROFILE%%\AppData\Local\lxss\bash.ico,0" /f
reg add HKCR\sh_sh\shell\open\command /ve /t REG_SZ /d "\"%SystemRoot%\System32\bash.exe\" --login -c \"/mnt/c/sh.sh '%%1'\"" /f
@echo.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment