Shell script to open a URL

BashShell

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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

Solution 3 - Bash

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.

Solution 4 - Bash

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

Solution 5 - Bash

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"

Solution 6 - Bash

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

cygstart https://stackoverflow.com

Solution 7 - Bash

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.

Solution 8 - Bash

start "" "browser_location" "address"

For example:

start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com"

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
QuestiondarecoderView Question on Stackoverflow
Solution 1 - BashEugene YarmashView Answer on Stackoverflow
Solution 2 - BashsjsamView Answer on Stackoverflow
Solution 3 - BashSangram NandkhileView Answer on Stackoverflow
Solution 4 - BashRoc BoronatView Answer on Stackoverflow
Solution 5 - Bashtrve.fa7adView Answer on Stackoverflow
Solution 6 - BashWayneJohnView Answer on Stackoverflow
Solution 7 - BashAdrian BartyczakView Answer on Stackoverflow
Solution 8 - BashJithin AntonyView Answer on Stackoverflow