从 Windows Subsystem for Linux 在主机系统中打开浏览器?

jan*_*pio 38 browser bash ruby windows-subsystem-for-linux

我有一个包含这一行的 ruby​​ 脚本:

system("open '#{html_path}'")
Run Code Online (Sandbox Code Playgroud)

html_path是本地.html文件的路径。因此,脚本会打开一个浏览器,我通常可以在其中检查和查看此文件。

与 MacOS 环境相比,这在bashWSL 中不起作用。运行脚本没有任何反应,当我open直接在控制台中执行调用时,我得到了这个:

sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ open Preview.html
Couldn't get a file descriptor referring to the console
Run Code Online (Sandbox Code Playgroud)

我已经调查open过在 Ubuntu做了一些不同的事情,我应该使用seexdg-open打开一个文件。

不幸的是,这些在 WSL 中也不起作用:

sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ see ./Preview.html
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /us
r/bin/see line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
Run Code Online (Sandbox Code Playgroud)

sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ xdg-open ./Preview.html
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /us
r/bin/run-mailcap line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening './Preview.html'
Run Code Online (Sandbox Code Playgroud)

仔细想想,这是有道理的:WSL 中没有可用的浏览器。

我可以以某种方式设置这个BROWSER变量以便seebashWSL内工作吗?


额外问题:如果是,如何在更改代码的情况下使 ruby​​ 脚本工作?这是一个外部依赖:/

小智 26

您可以设置BROWSER变量。有一篇很长的文章解释了如何设置环境变量

如果要为当前终端会话设置变量,可以使用:

export BROWSER='/mnt/c/Program Files/Firefox/firefox.exe'

(假设您想使用 Firefox 并将其安装在C:\Program Files\Firefox\firefox.exe

如果您希望这是持久的,您可以将上面的行添加到文件中~/.bashrc

  • 您的回答实际上并没有告诉我如何在设置`BROWSER` 环境变量后打开文件... (8认同)
  • 我必须在路径字符串中的空格之前添加 \ 才能正常工作。 (5认同)
  • 这对我不起作用,因为它直接使用linux路径,例如file:///tmp/cover688282190/coverage.html (2认同)

小智 24

安装wslu并使用 'BROWSER=wslview'

  • 这可能应该被标记为答案——这是作为 ubuntu-wsl 的一部分预装在 Ubuntu 上并由规范工程师提供的。您可以在此处查看有关它的博客文章:https://ubuntu.com/blog/new-installation-options-coming-for-ubuntu-wsl。虽然设置 `$BROWSER` envvar 实际上也解决了这个问题,但使用 `wslview` 允许在 WSL 和 Windows 环境之间进行可预测的桥接。我通常还会首先参考 https://wiki.ubuntu.com/WSL,其中包含任何特定于 Ubuntu 的内容,因为它经常更新。 (3认同)
  • 不幸的是,此设置不适用于将文件作为参数的“xdg-open”,例如“xdg-open index.hml”。在本例中,Windows 的“开始”以原始形式获取 Linux 路径:“系统找不到指定的文件。” ... `+ Start "/home/user/tmp/index.html"` (2认同)
  • `wslview` 需要运行 powershell,这非常慢 - 在我的计算机上打开网址会延迟几秒钟。另一方面,“explorer.exe”是即时的,并根据 Windows 设置使用默认浏览器打开 URL。 (2认同)

小智 14

出于某种原因,WSL 可以看到 windows 文件资源管理器。如果你这样做:

explorer.exe Preview.html 
Run Code Online (Sandbox Code Playgroud)

在控制台中,它应该在默认的 Windows Web 浏览器中打开。

  • 它只为我打开 Windows 资源管理器中的 Documents 文件夹 (3认同)

小智 9

结合上面的两个答案,最简单的解决方案是将 BROWSER 设置为 windows 文件资源管理器,以便使用默认的 Windows Web 浏览器

export BROWSER='/mnt/c/Windows/explorer.exe'
Run Code Online (Sandbox Code Playgroud)

  • 使用 `update-alternatives --install "bin/host_chrome" "chrome" "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" 1` 然后是 `export BROWSER=host_chrome`给我把戏。 (2认同)

Mic*_*ole 6

sensible-browser http://localhost:8001/在带有 WSL2 的 Ubuntu 20.04 上使用 过

  • 这个答案应该得到所有的赞同。无需配置任何东西。它就是有效的! (4认同)
  • 这在 Ubuntu 22.04 / WSL2 中不起作用(无需设置任何内容): *找不到合适的网络浏览器!将 BROWSER 环境变量设置为您所需的浏览器。* (2认同)

Joh*_*hat 5

这是open来自 macOS 的 WSL的“穷人” :

alias open="powershell.exe /c start"

将其添加到您的~/.profile~/.bashrc适当的,或在您的 WSL shell 中运行它,然后以下事情将起作用:

open . # opens current folder in Explorer as e.g. \\wsl$\Ubuntu\home\john\myapp
open foobar.txt # opens in notepad.exe
open README.md # opens in VSCode in Windows, for example
open http://example.com # opens in your default web browser in Windows
open Preview.html # opens in your default web browser in Windows, as file://wsl%24/Ubuntu/home/john/Preview.html
Run Code Online (Sandbox Code Playgroud)

缺点:这似乎只适用于相对路径,而不适用于绝对路径

如果有人制作了一个 shell 脚本来模拟macOS 版本,例如-a应用程序、-e编辑器、-R在 explorer.exe 中显示等,并使其使用绝对路径,那就太好了

  • 要在 Windows 和 WSL 路径之间进行转换,只需使用 `wslpath` (2认同)

Xar*_*ses 5

基于先前的答案

export BROWSER=/some/path/to/chrome.exe可能是您正在寻找的解决方案,但是 chrome 的默认路径是/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe(带有空格和括号的路径,用于外壳转义)。如果应用程序使用带引号的变量版本"${BROWSER}"将成功打开 chrome ,这将正常工作。但是,如果您使用变量 unquoted $BROWSER,bash 会将其视为单个变量。这会破坏某些工具,例如xdg-open.

解决这个问题很简单,您可以将其符号链接到没有空格的路径。

ln -s "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" ~/.local/bin/chrome
export BROWSER=~/.local/bin/chrome
Run Code Online (Sandbox Code Playgroud)

或者,您可以update-alternativesBrett指出的那样使用。最后,这也是一个符号链接,但在何时/是否需要更新链接指向的位置时具有更高的可见性。

update-alternatives --install "bin/host_chrome" "chrome" "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" 1
export BROWSER=host_chrome
Run Code Online (Sandbox Code Playgroud)