123

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
nicoulaj
  • 3,463
  • 4
  • 27
  • 32

6 Answers6

114
python -mwebbrowser http://example.com

works on many platforms

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 3
    If the user has Python installed... But thanks for mentioning the `webbrowser` module ! – nicoulaj Jun 26 '10 at 17:41
  • 2
    @JulienNicoulaud I just installed debian and python is there. I think it's bundled with many linux distributions. – Tomáš Zato Oct 08 '15 at 08:14
  • It's a nice module, but it doesn't seem to work with local files. This would be a common use case when doing web development. – Hexatonic Nov 01 '15 at 16:02
  • 4
    @Hexatonic: It does work with local files e.g., `python -m webbrowser file:///usr/share/doc/python/FAQ.html` – jfs Sep 07 '16 at 15:51
88

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"
Philipp
  • 48,066
  • 12
  • 84
  • 109
  • Oh yes, of course. Thanks. (First I'd have liked to use the `-s` option, but that doesn't seem to exist on Linux.) – Philipp Jun 26 '10 at 17:29
  • 5
    Isn't it [*bad to use `which` to detect binaries*](http://stackoverflow.com/a/677212/114900)? – msanford Jan 14 '14 at 16:09
  • 4
    I did the minus one for saying eval is evil. This is bash, not javascript. Catch phrases don't carry across programming languages. – Darth Egregious Dec 14 '15 at 18:02
  • 5
    except it's not available in OSX – gka May 22 '17 at 15:56
  • 1
    Beautiful use of bash (2nd version) ! learned something new today. – brokenfoot Nov 11 '17 at 00:41
  • about xdg-open and xdg-utils, isnt it only for debian or available for other linux distros too? – Abhishta Gatya Mar 16 '18 at 08:57
  • 3
    @DarthEgregious, Your downvote is misplaced. `eval` [is dangerous](https://stackoverflow.com/q/17529220/2417578) in shell scripts. – sh1 Apr 12 '18 at 17:03
  • `xdg-open` also works in a Linux distro on Windows Subsystem for Linux (WSL). I tested `xdg-open .` with Ubuntu 20.04 as the distro, and it opened the folder in the Windows File Explorer. From there it was easy to use the default web browser to load the HTML files that I happened to have in that directory. – Manfred Dec 31 '21 at 00:35
63

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
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
mbs400
  • 667
  • 5
  • 2
19

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.

Joan Rieu
  • 999
  • 8
  • 19
1

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
james-see
  • 12,210
  • 6
  • 40
  • 47
-9

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.

Raphi
  • 390
  • 2
  • 5