[root tip] [Utility Script] Setting the default web-browser

Setting web browser script

How do I set the default browser?

How often have we heard the question?

I just came across an issue where my development environment did not open the expected browser.

This occurs when the mime settings is out of sync with each other. To get my system to behave I put together this tiny script.

Create the file and make it executable (you may need to create the bin folder beforehand)

mkdir -p ~/.local/bin
touch ~/.local/bin/set-browser.sh && chmod +x ~/.local/bin/set-browser.sh

Then edit the file with your favorite editor and copy-paste below into the file and save it.

# Setting default browser using xdg utils
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
# 2021-11-20
# @linux-aarhus (c) root.nix.dk
# https://root.nix.dk/en/utility-scripts/set-browser-script

    echo "Usage:"
    echo "  Supply browser desktop file as the first argument"
    echo "  e.g.   set-browser.sh firefox.desktop"
    echo "" 

    echo "Available browsers (installed)"
    echo "---"
    # funny thing with cut when splitting a path
    # one would expect the column to be number 4 but it is 5
    # as string starts with `/` and therefore first column is empty
    # e.g. /usr/share/applications/firefox.desktop
    grep -rl 'internet' /usr/share/applications | cut -d'/' -f5
    echo "---"  

set_browser() {
    # set scheme handlers
    xdg-mime default "$1" x-scheme-handler/https
    xdg-mime default "$1" x-scheme-handler/http
    # set default browser
    xdg-settings set default-web-browser "$1"
    echo "Default browser set using $1"

# check if argument is supplied
if [[ "$1" == "" ]]; then
    exit 1

# check if desktop file
if [[ "$1" =~ ".desktop" ]]; then
    # check if the file exist
    if [[ -f "/usr/share/applications/$1"  ]]; then
        set_browser "$1"
        echo "$1 - file not found"
        exit 1
    echo "$1 is not a .desktop file"
    exit 1

The script requires you to know the desktop file name for the browser in question - run the script without argument to get a list of available browser launchers.

You can find the correct name by searching the folder /usr/share/applications as this example shows.

The command searches the file contents for the string internet which indicates a browser applications, pipes the result through cut and print only the last column. Then use the appropriate file as argument for the script.

$ grep -rl 'internet' /usr/share/applications | cut -d'/' -f5

To set the browser run the script like this

set-browser.sh microsoft-edge-dev.desktop

Source: Set Browser Script | root.nix.dk

This is awesome!

It can also be used to set other applications as default with a bit of modification. But it’s awesome for its purpose!

arrays start at ? … :wink:

That was my first thought - zero based index but that would skew it even more

The delimiter / creates a column - and because there is no data preciding the first column - cut creates an empty column - not what I initially expected but it is logical to a computer :grin:

$ echo /usr/share/applications/firefox.desktop
$ echo /usr/share/applications/firefox.desktop | cut -d'/' -f1

$ echo /usr/share/applications/firefox.desktop | cut -d'/' -f5

had the string been space delimited the column would have been 4

$ echo usr share applications firefox.desktop
usr share applications firefox.desktop
$ echo usr share applications firefox.desktop | cut -d' ' -f1
$ echo usr share applications firefox.desktop | cut -d' ' -f4

Maybe it makes it more intelligible to say "the 5th thing after a /"

IDK … to me it makes most sense as a sort of wrapper … where the ‘ends’ are what creates the whole


The first / is #1 and if you use 1 it will print up to that, where using 5 will print everything after the 4th / … it sort of makes sense in a bell-curve sort of way.
And yes - I honestly believe it is this way because people complained about the more logical version.
Have I researched this? No … but I am going to run with it :laughing:

1 Like

It is possible to create the same confusion by doing like this

echo ' usr share applications firefox.desktop ' | cut -d' ' -f6

Yes it works the same - #1 is the up to the first delimiter.
I guess I should have said ‘should start on 0’…
(or that 1=0 sometimes…blargh)

Its probably worth noting that cut is for … cutting away pieces, not printing them.

So in your example if you add an extra space in the beginning it will work more like you expect … and otherwise as long as you assume you always start with 1 delimiter. As opposed to something like awk '{ print $5 }' which will actually print the fifth column.

1 Like

if i want last section, i use awk and $NF

grep -rl 'internet' /usr/share/applications | awk -F'/' '{print $NF}'

not useful here !
last column -1:

grep -rl 'internet' /usr/share/applications | awk -F'/' '{print $(NF-1)}'
1 Like