我正在尝试打开 http://localhost在(任何)来自 WSL bash 终端的浏览器中。
到目前为止我已经尝试过:
- > How can I open Google Chrome from the terminal with the URL "localhost:3000"?
- > "Couldn't find a file descriptor referring to the console" on Ubuntu bash on Windows
- > How to mention C:\Program Files in batchfile
没有为 xdg-open 设置 BROWSER 变量,它用 /usr/bin/xdg-open: 851:/usr 响应
。xdg-open http://localhost
/bin/xdg-open:/c/"程序:未找到
我试过使用 \
和 ^
进行转义。使用 %ProgramFiles(x86)%
和 ofcorse "Program Files (x86)"
。每次或多或少都是相同的答案...关于如何在 WSL 中设置打开浏览器的工作流程有什么想法吗?
到目前为止,我已经结束了:
/c/Program\Files\\(x86\)/Google/Chrome/Application/chrome.exe localhost
但我正在寻找更优雅的解决方案。
最佳答案
您可以从 Bash 调用 Windows 命令行,并使用 Windows 文件关联在默认 Windows 浏览器中打开 URL。
为此,只需输入 Bash:
cmd.exe /C start http://localhost
在我的例子中,这会在 Chrome 中加载 localhost
,请注意完整的 URL 是 Windows 决定要做什么所必需的。
这类似于 open
在 MacOS 中所做的,因此您可能会发现直接为命令设置别名并将其用于其他类型的文件很有用:
# add this to .bash_aliases
open='cmd.exe /C start'
现在您可以直接从 WSL 打开 URL
或 打开 file.pdf
。
注意:由于您只是将命令重定向到 cmd.exe
,因此它需要能够访问您正在使用的文件。因此,当您发现自己在 Windows 文件系统中时,上述解决方案将起作用,但当您使用 Linux 分区中的文件时(即在 tmp
或 bin 中)可能会失败
文件夹)。这大概has been fixed在新版本的 WSL 中,但我还没有测试过。
关于windows - WSL (Ubuntu) : how to open localhost in browser from bash terminal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691835/