Clean way to launch the web browser from shell script?

BashShellCommand LineStandardsStandards Compliance

Bash Problem Overview


In a bash script, I need to launch the user web browser. There seems to be many ways of doing this:

  • $BROWSER
  • xdg-open
  • gnome-open on GNOME
  • www-browser
  • x-www-browser
  • ...

Is there a more-standard-than-the-others way to do this that would work on most platforms, or should I just go with something like this:

#/usr/bin/env bash

if [ -n $BROWSER ]; then
  $BROWSER 'http://wwww.google.com'
elif which xdg-open > /dev/null; then
  xdg-open 'http://wwww.google.com'
elif which gnome-open > /dev/null; then
  gnome-open 'http://wwww.google.com'
# elif bla bla bla...
else
  echo "Could not detect the web browser to use."
fi

Bash Solutions


Solution 1 - Bash

python -mwebbrowser http://example.com

works on many platforms

Solution 2 - Bash

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"

Solution 3 - Bash

OSX:
$ open -a /Applications/Safari.app http://www.google.com

or

$ open -a /Applications/Firefox.app http://www.google.com

or simply...

$ open some_url

Solution 4 - Bash

You could use the following:

x-www-browser

It won't run the user's but rather the system's default X browser.

See: this thread.

Solution 5 - Bash

Taking the other answers and making a version that works for all major OS's as well as checking to ensure that a URL is passed in as a run-time variable:

#!/bin/bash
if [ -z $1 ]; then
  echo "Must run command with the url you want to visit."
  exit 1
else
  URL=$1
fi
[[ -x $BROWSER ]] && exec "$BROWSER" "$URL"
path=$(which xdg-open || which gnome-open) && exec "$path" "$URL"
if open -Ra "safari" ; then
  echo "VERIFIED: 'Safari' is installed, opening browser..."
  open -a safari "$URL"
else
  echo "Can't find any browser"
fi

Solution 6 - Bash

This may not apply exactly to what you want to do, but there is a really easy way to create and launch a server using the http-server npm package.

Once installed (just npm install http-server -g) you can put

http-server -o

in your bash script and it will launch a server from the current directory and open a browser to that page.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnicoulajView Question on Stackoverflow
Solution 1 - BashjfsView Answer on Stackoverflow
Solution 2 - BashPhilippView Answer on Stackoverflow
Solution 3 - Bashmbs400View Answer on Stackoverflow
Solution 4 - BashJoan RieuView Answer on Stackoverflow
Solution 5 - BashjamescampbellView Answer on Stackoverflow
Solution 6 - BashRaphiView Answer on Stackoverflow