WSL: Execute a Command in a New Window and Close When Complete

This blog post describes a technique that you can use to open a new window to run a Windows Subsystem for Linux (WSL) command and close that window automatically afterwards. You can use a similar technique for Windows command lines.

If you do not already have a WSL shell window open, you can open a shell window, enter a command line, and close the window afterwards. Alternatively, you can:

  • Press Ctrl+R
  • Type wt wsl followed by the command line
  • Press <Enter>

Ctrl+R opens the run dialog, wt.exe launches Windows Terminal that runs wsl.exe (bash.exe, the Windows Subsystem for Linux Bash shell) to invoke the remainder as a command line before closing the Windows Terminal window (unless an error occurs).

For example, to get the man page for the sort command:

Ctrl+R,wt wsl man sort,<Enter>

If you don’t mind using a Windows command prompt instead of Windows Terminal, you can omit wt:

Ctrl+R,wt wsl man sort,<Enter>

As a bonus, unless you disable this feature, the Windows run dialog keeps a history of the commands that you invoke.

For a Windows command line, you can achieve the equivalent by adding the /c argument to cmd.exe. For example:

Ctrl+R,cmd /c pause,<Enter>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: