How to launch a new WSL bash window from an existing WSL bash window
Asked Answered
A

8

20

I am struggling to figure out how to launch a new "command prompt" window via a bash command in WSL. The goal is to launch a second prompt preferably already in bash.

I have already tried running cmd.exe yet that just drops me to a standard windows command prompt cmd from with in the bash shell.
https://i.sstatic.net/f8leY.png

Running the bash.exe or wsl.exe commands just takes me to another bash shell from within that same bash shell.
https://i.sstatic.net/kA9Cw.png

I am using the Debian distribution for WSL though that should not matter.

I know you can make a new window from the standard command prompt by putting the start command before the program. Yet I can not find the *nix equivalent or how to call a bash command from within WSL that does the same thing.

Alfreda answered 25/4, 2019 at 5:28 Comment(6)
cmd.exe /c start bash, but this question is off topic here.Ethno
Also, clear concepts lead to language that's less contorted. You're starting bash in a new console. "command prompt" is a weird expression. Sometimes people use it to refer to the console, and sometimes they use it for the CMD shell. They're not the same thing, so it's never clear what people mean, or even whether they understand that they're not the same thing.Ethno
The above command runs CMD just to use its builtin start command, which defaults to calling WINAPI CreateProcessW with the flag CREATE_NEW_CONSOLE. That sets the bash.exe console handle to a sentinel value that tells it to allocate a new console on startup instead of inheriting the parent's console. There's nothing inherent to this that requires using cmd.exe, or even a console application. For example, if Windows Python is installed, you could use pythonw.exe -c "import subprocess; subprocess.Popen('bash', creationflags=subprocess.CREATE_NEW_CONSOLE)". It's just a lot more verbose.Ethno
Thanks the first command is what I needed.Alfreda
cmd.exe /c start bash will again start the default wsl distribution with the default user which may not be what you want. If you don't have python or you don't want to be burdened with it the correct way to go about it is to start another wsl instance for same distribution and same user like I show in my answer.Scrobiculate
But why do you want to "aunch a new "command prompt" window"? That's fine and all, but can't we launch it like we launch Ubuntu from the Start menu, i.e. with the Ubuntu icon and the correct window name, i.e. to launch it in a default way? Because in the way you are saying, you end up with a Cmd.exe icon and no correct title name ...Sexism
S
23

This works from a bash prompt in WSL.

cmd.exe /c start cmd.exe /c wsl.exe

(Solution found here)

Siegel answered 8/1, 2020 at 21:53 Comment(1)
if you have several distributions installed this will fire up your default one with the default user. This may not be waht you want. Also there is no need to call cmd twice.Scrobiculate
H
5

In WSL 2 this works:

$ cmd.exe /c start wsl.exe
Hardecanute answered 19/1, 2023 at 1:59 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bevon
S
1

There is no need to call cmd twice. I use this kind of thing:

cmd.exe /c start wsl.exe -d $WSL_DISTRO_NAME --user $LOGNAME -- ssh -p 23 -L 80:10.0.0.1:80 -L 443:10.0.0.1:443 username@remoteserver

this will open another wsl window for the same user and distribution of the caller window and start ssh session in it. The ssh session can be replaced with whatever you want to run in the new window ... if you want just the shell prompt omit everything after $LOGNAME

Scrobiculate answered 9/9, 2021 at 15:21 Comment(2)
But this opens in a Command Prompt window with a Command Prompt icon. That's fine and all, but can't we launch it like we launch Ubuntu from the Start menu, i.e. with the Ubuntu icon and the correct window name, i.e. to launch it in a default way?Sexism
For me produces another icon in the taskbar just like the parent (fedora in my case). The icon in the window description is a generic penguin instead of the fedora icon in the window that launched it. In any case I don't get the CMD icon anywhere.Scrobiculate
R
1

2024 Update using Windows Terminal

How to launch WSL bash window from WSL bash

  • Install Windows Terminal

  • Launch new window:

    wt.exe --window new --profile "Ubuntu-20.04" bash -ic "npm start:server"
    
  • Launch new tab:

    wt.exe --window last new-tab --profile "Ubuntu-20.04" bash -ic "npm start:server"
    

Important notes

  • Using bash -i (interactive) is required
  • Be sure the default WSL 2 distribution matches the --profile distribution.
$ wsl.exe -l
Windows Subsystem for Linux Distributions:
Ubuntu-20.04 (Default)
Ubuntu-18.04
Debian

$ wsl.exe --set-default Ubuntu-20.04 # If not already set.
  • To launch the new bash shell in a particular directory use:
--startingDirectory . # current directory

Bonus features

  • Set the window/tab title
--title "My Server"
  • Set the tab color (RGB)
--tabColor "#0F0" # Green

Example 1: Command as an npm script in package.json

  "start:window": "wt.exe --window new --profile \"Ubuntu-20.04\" --title \"My Server\" --tabColor \"#0F0\" --startingDirectory . bash -ic \"npm start:server\"",

Example 2: Bash script to launch multiple windows

#!/bin/bash

wt.exe --window new --profile "Ubuntu-20.04" --title "Firebase Emulators" --tabColor "#00F" --startingDirectory . bash -ic "npm start:firebase"
sleep 15 # Seconds to wait for the Firebase emulators to start

wt.exe --window new --profile "Ubuntu-20.04" --title "My Server" --tabColor "#0F0" --startingDirectory . bash -ic "npm start:server"

References

Reddick answered 15/2 at 3:9 Comment(0)
B
0

The only way you're going to launch a new window from WSL is if you use some sort of terminal emulator and launch it. There are a number of different options: wsltty, wsl-terminal, Conemu, and Cmder (which is based upon Conemu) all come to mind offhand. Wsltty is based upon mintty which is the default terminal emulator for Cygwin, which is what people used before WSL came long. I personally like Cmder, but I found set up to be painful. YMMV.

Berryman answered 27/4, 2019 at 18:4 Comment(1)
The first comment posted by eryksun is exactly how you accomplish what I asked. He did not submit it as a answer so I could not mark this as solved (as far as I know). Though I do appreciate the terminal emulator suggestions. I have been a Cmder user for a couple years.Alfreda
W
0

even better one if you want to open a certain distro go to the explorer type shell:AppsFolder find your distro make a shortcut to your desktop and do this within your wsl

$ cmd.exe /c <absolute path of that shortcut>

eg:

$ cmd.exe /c "C:\Users\fake user which i just made up\Desktop\Ubuntu.lnk"

you can also copy the shortcut to somewhere and use the absolute path instead of only distro name you might need to use "" enclosing the path

edit : if you find yourself having this error CMD.EXE was started with the above path as the current directory.UNC paths are not supported i couldn't find any solution to that you can use this docs to cmd.exe params

Wilburwilburn answered 21/7, 2021 at 10:25 Comment(0)
A
0

(Note you need to install Windows Terminal if you don't already have it in order for this solution to work).

So none of these answers really worked for me. Turns out the best solution for me was to use cmd.exe to open bash through windows terminal (wt.exe), ie:

cmd.exe /c "wt.exe" -p "Debian"

Use -p to indicate the profile name of your command-line app, or leave it to use the default.

You can use this to open in a new tab and to pass commands as well:

cmd.exe /c "wt.exe" -w 0 nt -p "Debian" python example.py

Official Microsoft documentation

Antagonism answered 20/4, 2022 at 3:21 Comment(0)
V
0

This is another option and the window stays open

cmd.exe /c start bash -c 'sleep 10'

Valueless answered 15/7, 2023 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.