55

Is there a command I can use in Bash that will open a specific URL in the default browser?

5 Answers 5

71

There's a command that knows about your default browser:

xdg-open http://google.com

This will also work for every other type of URI (Uniform Resource Identifier), like images - which will automatically open with eog, openoffice documents, and so on, and also on filesystem paths (xdg-open /tmp/foobar.png).

There is also

xdg-email [email protected]

and

xdg-mime query default text/html

To find out what application is going to be used.

You can set your preffered application by going to System → Preferences → Preferred Applications:

alt text

These utilities are part of the freedesktop.org specification, so you can use them in your applications - and it's going to work on all of the desktops that adhere to the spec.

1
  • 1
    echo alias open=xdg-open >> ~/.bashrc, highly recommended.
    – ulidtko
    Aug 27, 2019 at 13:58
11

You can use xdg-open like so:

xdg-open http://askubuntu.com/

From the XDG-OPEN man page:

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.

4

If you don't necessarily want to use the default browser, you can call any browser program directly and give the URL as argument, for example:

chromium-browser https://youtube.com

or

firefox https://youtube.com
3

gnome-open works as well as xdg-open but neither knows what to do with naked domain.

So gnome-open http://askubuntu.com works but not gnome-open askubuntu.com.

Here is a small function to make it easier to type and accept the stripped down domain name.

function go { gnome-open http://$1 ; }

Paste the above in your command line, hit enter and try it out. If you like it you can save it to your .bash_aliases or .bashrc file, then source ~/.bash_aliases (assuming it is in your home directory) and it will be persistent for that session and all new sessions.

So now we just type go askubuntu.com and wallah!

1
  • gnome-open is not found on Ubuntu 23.10 with GNOME 45
    – Flimm
    Nov 15, 2023 at 9:17
1

Using x-www-browser URL will open the browser or a new tab at the given URL

1
  • x-browser is not found in Ubuntu 23.10
    – Flimm
    Nov 15, 2023 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .