I am trying to open http://localhost in (any) browser from WSL bash terminal.
So far I have tried:
No luck in setting up BROWSER variable for xdg-open, it responds to xdg-open http://localhost
with /usr/bin/xdg-open: 851: /usr/bin/xdg-open: /c/"Program: not found
.
I have tried escaping with \
and ^
. Using %ProgramFiles(x86)%
and ofcorse "Program Files (x86)"
. More or less it is the same answer every time... Any ideas how to set a work flow for opening browser in WSL?
So far I've ended up with:
/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe localhost
But I am looking for more elegant solution.
WSL translates the Linux system calls into windows ones so the Ubuntu network data flows through the exact same TCP/IP stack as the windows data. In short this means to access the Linux localhost you just access the windows one, they are the same. localhost:4567 or 127.0. 0.1:4567 will do what you want.
This can be achieved using the wslview utility and setting the $BROWSER environment variable to 'BROWSER=wslview'. wslview is a WSL browser emulator that can help you open links in the default Windows browser or open files on Windows from WSL.
I created a script that basically forwards xdg-open
to powershell -c start
Not tested much though.
sudo tee /usr/local/bin/xdg-open <<EOF
#!/bin/sh
powershell.exe -c start "'\$@'"
EOF
sudo chmod +x /usr/local/bin/xdg-open
Cheers Oliver
You can invoke the Windows command line from Bash and use Windows file association to open URL with the default Windows browser.
To do so just type in Bash:
cmd.exe /C start http://localhost
In my case this loads localhost
in Chrome, note that the full URL is necessary for Windows to decide what to do.
This is similar to what open
does in MacOS, hence you may find useful to directly alias the command and use it also for other type of files:
# add this to .bash_aliases
open='cmd.exe /C start'
Now you can open URL
or open file.pdf
directly from WSL.
Note: since you are simply redirecting commands to cmd.exe
, it needs to have access to the file your working with. As a consequence the above solution will work when you find yourself in the Windows file system, but probably will fail when you are working with files in Linux partition (i.e. in the tmp
or in the bin
folder). This probably has been fixed in the new version of the WSL but I have not tested it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With