Option 1: Set the default "Open with ..." for a .sh
file:
In order to establish a default double-click action, I believe you'll need to use this method.
To pass the correct command-line into WSL, we'll first need to pass it through something that can handle Windows paths. For that, we'll use a simple Windows script file (.cmd
). Place it where you like; perhaps something like %userprofile%\Documents\scripts\WSL Launcher.cmd
. Add the following:
@echo off
wsl -e sh -c "\"$(wslpath '%1')\""
PAUSE
That runs WSL with the POSIX (or closest equivalent for your distribution) shell, converts the Windows path that is passed in to a Linux path, and attempts to execute it.
Also, while we're at it, create %userprofile%\Documents\scripts\test.sh
for trying this out with the following contents:
#!/usr/bin/env bash
echo "Hello from WSL Bash"
Side-note: Make sure to create this file with Linux line-endings and set it to be executable.
Then:
- Right-click on the
test.sh
file
- Open With -> Choose Default Program -> More Apps -> Look for another app on this PC
- Browse to the
WSL Launcher.cmd
you created and Open.
You should see the proper output, with a "Press any key to continue ..."
Of course, to set it to the default double-click action, repeat the process above, but check off the "Always use this app to open .sh files" option before you "Look for another app on this PC".
Side-note: As mentioned, Linux doesn't care about file extensions, so this CMD will really run anything that can be executed at the Linux command-line that has a .sh
extension. You can even do something intentionally crazy like rename ls
(or any binary) to ls.sh
and this script will execute it.
This is probably not a security risk, since anything that you can run that way could just as easily be executed by just running it inside the shell script anyway; it's just something to be aware of.
Option 2: Associate a right-click action with .sh
files or other file extensions to run scripts on them.
This won't get you the double-click ability, but if you want a more flexible way to handle right-click actions that launch WSL, that can be done via Windows shell actions. Shell actions can be attached to either a particular extension or to the wildcard ("*") to run against all files.
For instance, you mentioned setting up zip
/unzip
, so I'd refer you to this answer which includes (in the bottom half of the answer) the steps to set up a script to handle .lrz
files inside WSL using the right-click menu in Windows.
This can be extended to the same concept as the "Open With" above. You'd just need to change the command-line. Because shell actions can use arbitrary command-lines with arguments, you don't need to set up an intermediary .cmd
script.