Code Self Study Forum

How to open multiple webpages in a browser automatically

Someone asked a question about how to open multiple web pages in browser tabs at once. I’ll post an answer here in case anyone else is searching for the answer online.

This method will work on Linux and Mac, and possibly Windows if you’re using WSL — basically anywhere bash scripts will run.

The Script

Create a file called open_urls.sh with the following contents:

#!/bin/bash

# put your URLS here, one on each line
URLS=(
    https://codeselfstudy.com/
    https://forum.codeselfstudy.com/
    https://twitter.com/codeselfstudy
)

firefox "${URLS[@]}"

Put one URL per line in the URLS array.

Save the file and enable “execute” permissions on it:

$ chmod +x open_urls.sh

Then run the script by typing out the path to the file like this:

$ ./open_urls.sh

Firefox will open all of the given URLs, each in a different tab.

Tip: if you want to open multiple local HTML files, you can do it like this:

$ firefox *.html

Explanation of the Code

This is the syntax for creating an array in bash:

URLS=(
    https://codeselfstudy.com/
    https://forum.codeselfstudy.com/
    https://twitter.com/codeselfstudy
)

After you’ve defined a variable in bash, you prefix a dollar sign to it to access its value. The variable is defined as URLS and is accessed as $URLS.

The last line here shows how the syntax will print out each item of the array on one line:

URLS=(
    https://codeselfstudy.com/
    https://forum.codeselfstudy.com/
    https://twitter.com/codeselfstudy
)

echo "${URLS[@]}"

So, to pass the firefox command the URLs on one line, you can just do this:

firefox "${URLS[@]}"

See this quick guide for more details on the syntax:

2 Likes

Thanks Josh. You can also use chromium-browser as a command to open the pages in chromium, if you prefer chromium to firefox. :slight_smile:

1 Like