1

I've set up my wsl with Ubuntu and installed a few simple applications like zip/unzip.

I have a simple shell script and am trying to set it to open automatically using WSL, but am having trouble finding the option.

If I execute the shell script from the wsl command window, it works fine. However, I want to double-click it and have it run automatically.

Looking at the Open With... menu, I can't find WSL or Ubuntu as otpions.

Update:

I can call wsl [command] from a batch file, but would prefer to execute the shell script.

1 Answer 1

5

In general, you'll need to configure this manually. Unfortunately, the "Open with ..." action always passes in a Windows path. Bash (or whatever shell you are running in WSL) doesn't understand Windows paths. Unlike Git Bash for Windows or Cygwin, this is the actual Linux bash binary that you are using, so it has no way to interpret Windows paths.

Also, an important different is that Windows determines the type of file (and the actions to take) based off the extension, but a shell script in Linux can be any extension.

I'm assuming that you'll be naming your shell scripts something like test.sh with an extension for this to work.

So we can work around this in at least two different ways:

  • 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.

2
  • In my case, it doesn't work: /usr/bin/env: ‘bash\r’: No such file or directory Press any key to continue . . .
    – tquang
    Commented Jun 22, 2023 at 6:54
  • @tquang The \r at the end indicates that you likely created the file in a Windows application that used the Windows-style CRLF. Try converting the file to Unix format LF or recreate it using a Linux tool as mentioned in the answer and let me know if that fixes it for you. Thanks! Commented Jun 22, 2023 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .