Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to launch the web browser from shell script?

People also ask

How do I access my browser shell?

You can run it by entering the URL https://server/phpshell/phpshell.php . Logging in with your username and password lets you start your shell session in your web browser. To begin, enter commands in the PHP Shell window (Figure 1).


python -mwebbrowser http://example.com

works on many platforms


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"

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

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.


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