I'm running Ubuntu with WSL1, and I want to make different shortcuts in Windows to run different commands on WSL startup, e.g., one to automatically start an SSH session, another to change to a specific directory. Once the startup command is executed, I need it to keep running like usual, but everything I've tried closes the terminal. I have tried something like

c:\windows\system32\bash.exe -c -i -s ssh ip

But it doesn't seem to do anything, or if it does, it closes way too quickly for me to see.


First up, I'm going to recommend that you use the newer (Microsoft recommended) wsl.exe command, rather than bash.exe. bash.exe is still available, but Microsoft calls it a "historical command" that has been "replaced by" wsl.exe. The wsl command also provides far more options than the older bash.exe.

The first problem you have is that there's a syntax/quoting error in your command line, so yes, the Windows Terminal tab is closing before you can read it. Best to run this from a PowerShell or CMD profile/tab first to be able to see the output:

PS> bash.exe -c -i -s ssh ip
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
           [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
           [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
           [-i identity_file] [-J [user@]host[:port]] [-L address]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] destination [command]
PS> bash.exe -c "ssh ip"
user@ip's password:
PS> wsl ssh ip
user@ip's password:
PS> wsl -e bash -c ssh ip
<displays usage information as above>
PS> wsl -e bash -c "ssh ip"
user@ip's password:

As you can see, if you are passing a command string to bash -c, it needs to be quoted. Otherwise, bash is just seeing the ssh without the ip.

The wsl command, on the other hand, does allow invocation directly without the quotes. It's basically smart enough to pass the remainder of the command-line (after all arguments/flags have been processed) into the default shell, quoted to the -c argument. You can see an example of this above with the wsl ssh ip.

Next you want to know how to keep the tab/shell open after running a command. This can be done several ways, but the easiest is to:

  • Start a shell
  • Run the first command
  • exec a replacement shell that will keep running

As you have experienced, just doing the first two of those will cause the shell to close when the command exits.

This will look something like:

wsl -e bash -c "ls; exec bash"
  • Comments are not for extended discussion; this conversation has been moved to chat. – Mokubai Apr 23 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.