Associating linux shell (.sh) scripts on Windows 10 to Bash or WSL [duplicate]
If you ever tried to run an .sh linux script on Windows 10 by double-clicking it, after assigning it to bash.exe (Ubuntu Bash for Windows 10) by default, you most likely noticed it wouldn't open.
Why doesn't it open, and how can I fix this?
Solution 1:
EDIT: the registry key and information below is OBSOLETE, please refer to the new version provided here: Can I drag and drop files to an .sh script using Bash on Ubuntu on Windows or Windows Subsystem for Linux (WSL)?
This can be done by editing a single registry key (or more for extra functionalities).
I will of course link a .reg file (actually three zipped reg files) to quickly install the workaround (along with icon association and (optional) nano edit enabler) without having to edit the registry yourself, if you download and install it, you must only follow step 1;
FILE (only follow step 1, then download and install): http://www.mediafire.com/file/r5uxk9c0hhucvt9/ShellBashScriptOpen_v3.rar
But to satisfy your healthy mistrust, here is how you can do it yourself:
.
1) Set bash.exe as default program for .sh files
Just right-click an sh file, go to properties, locate "open with", change > more_apps > find_an_app_in_this_PC and select C:\Windows\System32\bash.exe;
Apply and you are good to go.
.
2) Actual double-click to open key
create the following registry key with regedit
HKEY_CLASSES_ROOT\Applications\bash.exe\shell\open\command
a) If you want to keep bash open after script finished
set the (Default) value to
"C:\Windows\System32\bash.exe" -c " \"./$(grep -oE '[^\\]+$' <<< '%L')\"; cd ~; bash;"
b) If you want to close bash after script finished
set the (Default) value to
"C:\Windows\System32\bash.exe" -c " \"./$(grep -oE '[^\\]+$' <<< '%L')\";"
.
3) Open as admin option
create the following registry key with regedit
HKEY_CLASSES_ROOT\Applications\bash.exe\shell\runas\command
a) If you want to keep bash open after script finished
set the (Default) value to
"C:\Windows\System32\bash.exe" -c " sudo bash \"$(grep -oE '[^\\]+$' <<< '%L')\"; cd ~; bash"
b) If you want to close bash after script finished
set the (Default) value to
"C:\Windows\System32\bash.exe" -c " sudo bash \"$(grep -oE '[^\\]+$' <<< '%L')\";"
.
4) Assign default icon key (optional [but cooler])
create the following registry key
HKEY_CLASSES_ROOT\Applications\bash.exe\DefaultIcon
and set the (Default) value to
%USERPROFILE%\AppData\Local\lxss\bash.ico,0
.
5) Add right-click > edit option (with nano editor) (optional)
create the following registry key
HKEY_CLASSES_ROOT\Applications\bash.exe\shell\edit\command
and set the (Default) value to
"C:\Windows\System32\bash.exe" -c "nano -miST 4 \"$(grep -oE '[^\\]+$' <<< '%L')\""