78 lines
No EOL
3.3 KiB
Markdown
78 lines
No EOL
3.3 KiB
Markdown
# Use browser from Bash
|
|
|
|
If we are in a Bash terminal sometimes we need to open a browser. A good example is Argocd, which supports the SSO login type which asks the user details from a different service, which probably requires a web browser.
|
|
|
|
## Behind the scenes
|
|
|
|
Depending on the `BROWSER` env variable, the Argocd and others calls the binary that is in the `BROWSER`.
|
|
|
|
We can try it by hand, issuing the `sensible-browser` command.
|
|
```
|
|
adns@adns-dellbook:/mnt/c/Users/adns$ sensible-browser
|
|
Couldn't find a suitable web browser!
|
|
|
|
Set the BROWSER environment variable to your desired browser.
|
|
|
|
adns@adns-dellbook:/mnt/c/Users/adns$
|
|
```
|
|
|
|
We can not open a browser. Some programs, E.G. rclone are ready to work in headless environments but some others, E.G. Argocd not, so we need to emulate a browser.
|
|
|
|
## Create the shell script
|
|
|
|
We'll write an easy-to-use shell script that prints out the URL that tried to open by the APP. With this way we can use it in headless environments.
|
|
```
|
|
#!/bin/bash
|
|
|
|
input="$@"
|
|
echo $input
|
|
read
|
|
```
|
|
|
|
Place in this script in your local bin folder. Create it with `mkdir -p ~/.local/bin` and for example, open it in nano, `nano ~/.local/bin/openbrowser`, paste the contents and give the read+execute right `chmod 770 ~/.local/bin/openbrowser`.
|
|
|
|
Add this line to your `~/.bashrc` to add the new bin to `$PATH`.
|
|
Line: `export PATH=$PATH:~/.local/bin`
|
|
|
|
Set the `BROWSER` env variable to use it with `export BROWSER=~/.local/bin/openbrowser`.
|
|
|
|
And try it out
|
|
```
|
|
adns@adns-dellbook:~/.local/bin$ sensible-browser https://theadam.eu
|
|
https://theadam.eu
|
|
|
|
adns@adns-dellbook:~/.local/bin$
|
|
```
|
|
|
|
> After printing the URL, press enter key to continue. Depending on the APP that is it need before filling data in browser or after.
|
|
|
|
## Open graphical browser
|
|
|
|
If you more than a Sherlock, you can determine from the prompts that it might be a WSL system so browser is available on the host OS.
|
|
|
|
Add the ability to the script to use your preferred browser.
|
|
|
|
Go to desktop, right click and properties on your browser shortcut. In my example it is the Chromium browser.
|
|
|
|
In my case Chromium shortcut is `C:\Users\adns\AppData\Local\Chromium\Application\chrome.exe --profile-directory="Default"`. I use different profiles but I want that if I open a new link, open in default profile. Change this based on your requirements.
|
|
|
|
WSL mounts the host OS in `/mnt` folder, the `C` drive very creatively mounted in `c` folder. (Yes, case-sensitive!).
|
|
|
|
Now convert the shortcut in this format. Replace the path to look as a Unix/Linux format and add the `/mnt`.
|
|
```
|
|
/mnt/c/Users/adns/AppData/Local/Chromium/Application/chrome.exe --profile-directory="Default"
|
|
```
|
|
|
|
Add this line to the script under echo and above the read.
|
|
```
|
|
/mnt/c/Users/adns/AppData/Local/Chromium/Application/chrome.exe --profile-directory="Default" "input"
|
|
```
|
|
|
|
And congrats, it will work. If you try to open the URL as in the previous example browser will open up.
|
|
|
|
If you want, you can remove the read or echo, it might be tested out which variant fits your requirements well.
|
|
|
|
## Sources
|
|
|
|
[https://wslu.wedotstud.io/wslu/man/wslview.html](wslview), which part of [https://github.com/wslutilities/wslu](wslu) and discontinued so not officialy available in the future.
|
|
[https://manpages.debian.org/unstable/sensible-utils/sensible-browser.1.en.html](sensible-browser(1)) |