Linux: Command to Open Url in Default Browser

Linux: command to open URL in default browser

The most cross-distribution one is xdg-open http://stackoverflow.com

open url with parameters in default browser

It actually works without the backslash. Turns out the backslash is needed in terminal, because the terminal internally escapes the backslash before sending the string to the browser. But in case of go, it just sends the original string to the browser.

In Go use:

exec.Command("open", "https://www.facebook.com/v11.0/dialog/oauth?client_id=123456&redirect_uri=https://example/com")

From the terminal command line:

open https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com

How can I open a site in web browser from command line?

Lets collect the various ways to open a URL the default browser:

Ubuntu

sensible-browser google.com
or
xdg-open google.com.

This is also answered here:
https://askubuntu.com/questions/8252/how-to-launch-default-web-browser-from-the-terminal

Windows

start chrome https://www.google.com/ or start firefox https://www.google.com/

Answered here: https://stackoverflow.com/a/32775952/3717691

MacOS

open "google.com"

Answered here: https://stackoverflow.com/a/23039509/3717691

Shell script to open a URL

Method 1

Suppose your browser is Firefox and your script urlopener is

#!/bin/bash
firefox "$1"

Run it like

./urlopener "https://google.com"

Sidenote

Replace firefox with your browser's executable file name.


Method 2

As [ @sato-katsura ] mentioned in the comment, in *nixes you can use an application called xdg-open. For example,

xdg-open https://google.com

The manual for xdg-open says

xdg-open - opens a file or URL in the user's preferred application
xdg-open opens a file or URL in the user's preferred application. If a
URL is provided the URL will be opened in the user's preferred web
browser.

If a file is provided the file will be opened in the
preferred application for files of that type. xdg-open supports file,
ftp, http and https URLs.

As [ this ] answer points out you could change your preferred browser using say:

xdg-settings set default-web-browser firefox.desktop

or

xdg-settings set default-web-browser chromium-browser.desktop

Clean way to launch the web browser from shell script?

xdg-open is standardized and should be available in most distributions.

Otherwise:

  1. eval is evil, don't use it.
  2. Quote your variables.
  3. Use the correct test operators in the correct way.

Here is an example:

#!/bin/bash
if which xdg-open > /dev/null
then
xdg-open URL
elif which gnome-open > /dev/null
then
gnome-open URL
fi

Maybe this version is slightly better (still untested):

#!/bin/bash
URL=$1
[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
echo "Can't find browser"

Script to Open URL as HTTP POST request (not just GET) in web browser

Sorry, but there's no reliable way to do this across browsers and across operating systems. This was a topic the IE team spent a bunch of time looking into when we built Accelerators in IE8.

The closest you can come is writing a HTML page to disk that has an auto-submitting form (or XmlHttpRequest), then invoking the browser to load that page which will submit the form. This constrains your code to only submitting forms that are legal to submit from the browser (e.g. only thee possible content types, etc).

Launching a web browser from command-line Dart script

Try this code:

import "dart:io";

void runBrowser(String url) {
var fail = false;
switch (Platform.operatingSystem) {
case "linux":
Process.run("x-www-browser", [url]);
break;
case "macos":
Process.run("open", [url]);
break;
case "windows":
Process.run("explorer", [url]);
break;
default:
fail = true;
break;
}

if (!fail) {
print("Start browsing...");
}


Related Topics



Leave a reply



Submit