如何在当前 powershell 目录中启动 WSL 终端?

Cia*_*lsh 3 powershell windows-subsystem-for-linux

我可以打开 powershell 并输入

> Ubuntu 
Run Code Online (Sandbox Code Playgroud)

在 Windows 10 上打开 WSL ubuntu shell。这将始终将我带到 WSL 主目录。我该如何在 powershell 当前所在的同一位置打开终端?

供参考。我需要这个来为 Windows 资源管理器创建右键单击“在此处打开终端”类型的注册表项。

Roa*_*ner 10

如果我们看看跑步ubuntu.exe实际上做了什么:

PS C:\> ubuntu.exe /?
Launches or configures a Linux distribution.

Usage:
    <no args>
        Launches the user's default shell in the user's home directory.

    install [--root]
        Install the distribution and do not launch the shell when complete.
          --root
              Do not create a user account and leave the default user set to root.

    run <command line>
         Run the provided command line in the current working directory. If no
         command line is provided, the default shell is launched.

    config [setting [value]]
        Configure settings for this distribution.
        Settings:
          --default-user <username>
              Sets the default user to <username>. This must be an existing user.

    help
        Print usage information.
Run Code Online (Sandbox Code Playgroud)

我们可以看到它默认在主目录中启动 WSL shell 。如果我们想在 PowerShell 中打开的当前目录中运行,我们需要指定该run选项。所以完整的命令是ubuntu.exe run.

另一种选择是只运行wsl.exebash.exe。默认情况下,这些命令将在当前工作目录中打开 WSL。

注意:我们不需要.exe在命令后面指定。运行ubuntuwsl一切bash正常。PowerShell 知道如何在不指定扩展名的情况下运行可执行文件。


小智 7

方法一:

  • 将 Ubuntu 设置为 wsl 默认发行版wsl --set-default Ubuntu
  • 只需输入 wsl,您就位于当前的 Powershell 目录中

方法2:

打开其他文件夹

  • wsl.exe --cd $pwd或者wsl.exe --cd "path"

在此输入图像描述

在此输入图像描述