82

How do I write a simple shell script (say script.sh), so that I can pass a URL as an argument while executing?

I want a browser to start with the page opened on that URL. I want to write the command in the script to open a browser and open the URL given in argument.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
darecoder
  • 1,478
  • 2
  • 14
  • 29

8 Answers8

138

You don't need to write a script for that. There're some tools that you can use depending on your OS:

Linux

xdg-open is available in most Linux distributions. It opens a file or URL in the user's preferred browser (configurable with xdg-settings).

xdg-open https://stackoverflow.com

macOS

open opens files and URLs in the default or specified application.

open https://stackoverflow.com
open -a Firefox https://stackoverflow.com

Windows

You can use the start command at the command prompt to open an URL in the default (or specified) browser.

start https://stackoverflow.com
start firefox https://stackoverflow.com

Cross-platform

The builtin webbrowser Python module works on many platforms.

python3 -m webbrowser https://stackoverflow.com

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • Any way to tell it which browser to try first, eg Chrome. On Mac it tries firefox then falls back to safari – Rob Sep 14 '17 at 15:34
  • If I type "open stackoverflow.com", then it uses my default browser – akohout May 14 '18 at 11:10
  • 1
    Thanks for this solution, now i can put this in auto start script and make sure that every time I start my PC It will open stackoverflow first and hopefully in 100 day I will get a golden badge :) – Ale Nov 07 '18 at 10:43
  • for google chrome it is: open -a 'google chrome' _yoururl_ – Yash Bedi Feb 24 '20 at 06:49
  • `python` is no longer cross-platform. Ubuntu 16.04 and above doesn't ship with Python 2 so it exits with `Command 'python' not found` – Alex Sep 11 '20 at 01:35
  • 1
    @Alex try this: `python3 -m webbrowser https://stackoverflow.com` – Hemant Kumar Nov 24 '21 at 11:41
33

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • 3
    On UNIX: `xdg-open "$@"` – Sato Katsura Jul 01 '16 at 14:23
  • 1
    but can we do something so that I can run it like ... urlopener google.com – darecoder Jul 01 '16 at 14:25
  • can we run that script like a command...without using "./" in the beginning and without using double quotes in giving URL. – darecoder Jul 01 '16 at 14:25
  • For that the path to your script should be added to the `PATH` environment variable. Check [\[ this \]](http://stackoverflow.com/a/14638025/1620779) Or copy the script to a standard binary folder though it is not the recommended method. – sjsam Jul 01 '16 at 14:40
  • is it a good practice to add my script file in /usr/bin/ folder?? – darecoder Jul 07 '16 at 11:54
  • Can create another PATH environment variable?.. If I do so, won't it affect the execution of other commands? – darecoder Jul 07 '16 at 11:55
  • You can't create another PATH environment variable. To add a new path to the existing path, the procedure is `PATH=$PATH:/your/new/path` – sjsam Jul 07 '16 at 16:12
  • You might get a warning about `xdg-open` being deprecated in GNOME 3. in that case, use `gio open "$@"`. Both tools work exactly the same :) – trve.fahad May 29 '17 at 16:15
12

For Windows,

You can just write start filename_or_URL

start https://www.google.com

It will open the URL in a default browser. If you want to specify the browser you can write:

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

Note: The browser name above can be obtained from the exe file found in program files (sample: C:\Program Files\Internet Explorer\iexplore.exe) if you wish to open multiple URLs.

start chrome "www.google.com" "www.bing.com"

It was tested with .sh (shellscript file) and .bat files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
8

In MacOS, just open works. So, open "$1" will open the passed URL in Chrome, if Chrome is the default browser.

Roc Boronat
  • 11,395
  • 5
  • 47
  • 59
6

If you want a cross-OS solution and are comfortable using Python (3):

Try this:

import webbrowser

webbrowser.open('https://yoururl.com')

Or in a terminal/cmd:

python -m webbrowser -t "https://yoururl.com"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
trve.fahad
  • 4,397
  • 2
  • 17
  • 25
1

For Cygwin under Windows you can't use start. But you can use cygstart:

cygstart https://stackoverflow.com
WayneJohn
  • 53
  • 6
0

January 2021: Huge script for Aliases!

Here is an awesome script for opening URLs with support for aliases and multiple browsers. Examples:

search|s|google|g
http://www.google.com/search?q={search\+}

search2|s2|yahoo
https://search.yahoo.com/search?p={search\+}

images|img
https://www.google.com/search?site=&tbm=isch&q={search\+}

videos|v|youtube|yt
https://www.youtube.com/results?search_query={search\+}

To use the Google aliased URL, you just run:

openurl search how to make fried rice

This will parse the "google" URL and open

https://www.google.com/search?q=how+to+make+fried+rice

See all the configuration options in the project!

Please note: This repository has zero very few stars, only because it's a subrepository of a parent repository: gnu-linux-shell-scripting.

Adrian Bartyczak
  • 188
  • 3
  • 14
-4
start "" "browser_location" "address"

For example:

start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com"
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • This is not a shell script. There are other answers here which include the same information but which correctly label it as a Windows-only variation and not actually an answer to the question at the top of this page. – tripleee Nov 06 '19 at 10:56
  • this is batch for ```dos cmd``` not bash script. – 钟智强 Aug 09 '21 at 02:46