buzoherbert
buzoherbert

Reputation: 1658

WSL - Launch bash from Powershell in new terminal and pass a command to it

I am trying to make a simple Powershell script to quickly setup my dev environment. For that I need a few instances of WSL programs running on bash terminals.

From Powershell, I am trying to:

  1. Open a new terminal window
  2. Start bash
  3. Run a command with bash - In my use case I just want to run a simple npm start within bash.

A plus is if I can do all of this in one script line.

I think I am close. If I use start powershell I can start a new terminal. That inmediately opens a new PowerShell terminal.

Then, I can pass PowersShell commands to it like so:

start powershell{bash}

This opens a new terminal window and immediately opens bash.

A way to pass commands to bash in PowerShell is like this:

bash -c "npm start"

This works well. It opens bash in the same terminal and then runs the command I am passing to it. npm start works just as if I was calling it directly from bash. The problem comes when I want to pass the npm start to the new terminal. This is what I am trying:

start powershell{ bash -c "npm start"; Read-host}

This opens the new powershell terminal and it seems to be opening bash. ; Read-host is added so that the terminal doesn't close immediately. However, instead of running the npm command, it reacts by showing me information about the npm command instead of actually running it.

enter image description here

Is there a workaround so that I can actually get the command to run in the new terminal window after opening bash?

Upvotes: 2

Views: 5826

Answers (1)

Laurent Gosselin
Laurent Gosselin

Reputation: 2221

I believe that the recommended way to start commands in the WSL environment is to use wsl.exe now. (https://learn.microsoft.com/en-us/windows/wsl/reference#wslexe)

Try start powershell{wsl -- npm start; Read-Host}

Upvotes: 4

Related Questions