调用WSL命令后,保持Windows终端选项卡打开

发布于 2025-02-11 04:44:56 字数 238 浏览 1 评论 0原文

我正在尝试在 Windows终端中打开 wsl(ubuntu)选项卡,然后使用WSL在该选项卡中运行命令。我使用以下powershell命令:

wt new-tab -p "WSL (Ubuntu)" wsl echo "hallo"

问题是在echo运行后,标签立即关闭。 有没有办法保持打开状态?

I'm trying to open a WSL (Ubuntu) tab in Windows Terminal, and then run a command in that tab using WSL. I use the following PowerShell command for that:

wt new-tab -p "WSL (Ubuntu)" wsl echo "hallo"

The problem is, after echo has run, the tab closes immediately.
Is there a way to keep it open?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2025-02-18 04:44:56
  • 当您将命令行传递到wt.exe,Windows终端CLI时,它是运行 的,无论您是否还指定特定的shell带有-p的配置文件。

  • 因此,要实现您想要的东西:

    • 您的命令行必须显式调用感兴趣的外壳...
    • ... ,外壳必须支持启动交互式,开放的会话与启动命令结合使用

虽然PowerShell支持这一点,但POSIX兼容的外壳(例如bash -WSL的默认外壳) - 不。

a-次优 - 解决方案是:

wt -p 'WSL (Ubuntu)' wsl -e bash -c 'echo ''hello''\; exec $BASH'

注意:

  • 莫名其妙,如Windows terminal v1.13.11431.0,
    ; char。在引用的-c参数中,需要逃脱为\; - 否则,它将通过wt.exe 用于打开多个 tabs/windows的分离器。


  • 以上执行指定的echo命令,然后使用exec 用交互式,开放的会话替换非相互交互的,自动关闭的原始Shell通过exec。这种方法的限制是,除了更换原始外壳时,启动命令所做的任何状态都会丢失。

    丢失。

    • 一个更好的解决方案是一个更好的解决方案:创建Bash初始化文件的临时副本,〜/.bashrc,附加启动命令,然后将临时副本的文件路径传递给bash's - rcfile option;此后删除临时副本。


  • When you pass a command line to wt.exe, the Windows Terminal CLI, it is run instead of a shell, irrespective of whether you also specify a specific shell profile with -p.

  • Thus, to achieve what you want:

    • Your command line must explicitly invoke the shell of interest...
    • ...and that shell must support starting an interactive, stay-open session in combination with startup commands.

While PowerShell supports this, POSIX-compatible shells such as bash - WSL's default shell - do not.

A - suboptimal - workaround is the following:

wt -p 'WSL (Ubuntu)' wsl -e bash -c 'echo ''hello''\; exec $BASH'

Note:

  • Inexplicably, as of Windows Terminal v1.13.11431.0,
    the ; char. inside the quoted -c argument requires escaping as \; - otherwise it is interpreted by wt.exe as its separator for opening multiple tabs / windows.

  • The above executes the specified echo command first, and then uses exec to replace the non-interactive, auto-closing original shell with an interactive, stay-open session via exec. The limitation of this approach is that any state changes other than environment-variable definitions made by the startup command(s) are lost when the original shell is replaced.

    • A better, but more elaborate solution is possible: create a temporary copy of Bash's initialization file, ~/.bashrc, append your startup commands, and pass the temporary copy's file path to bash's --rcfile option; delete the temporary copy afterwards.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文