0

I am trying to open various tab at the same time using xdg-open my default browser is firefox.

The following line works (for one tab):

xdg-open https://stackoverflow.com

But this one does not:

xdg-open https://stackoverflow.com https://google.fr

the error thrown is:

xdg-open: unexpected argument 'https://google.fr'

Isn't it any way of passing various URL to xdg-open? Thanks in advance !

Ced
  • 793
  • 4
  • 12
  • 31
  • You'll need to provide the actual browser name and urls separated by spaces. `xdg-open` doesn't do what you want. It takes a single argument. See `man xdg-open`. – DK Bose May 24 '19 at 08:27

2 Answers2

3

This is not possible with xdg-open alone because it expects exactly one argument. But you can write a function to iterate over all given arguments and call xdg-open separately.

Open your ~/.bashrc file in an editor, e.g.

gedit ~/.bashrc

and then add the following text at the end of the file:

xo () 
{ 
    for var in "$@"; do
        xdg-open "$var";
    done
}

Save the file and leave the editor. After that either close and re-open the terminal window or enter

source ~/.bashrc

in the current window for the change to take effect. From now on you have a new command xo and can issue

xo https://stackoverflow.com https://google.fr

See also my answer to the slightly related question Shorten or merge multiple lines of &> /dev/null &.

PerlDuck
  • 13,014
  • 1
  • 36
  • 60
  • Thanks, I decided to rename it `xdg-open-multi` – Ced May 24 '19 at 09:36
  • About `source ~/.bashrc` should I run it everytime before I wanna use `xdg-open-multi` ? – Ced May 24 '19 at 09:36
  • @Ced No. If you have put it in your `~/.bashrc` file, it is read (and the function is defined) every time you open a _new_ terminal. The `source ...` command is only neccessary when you changed (added) the definition and want to use it immediately in the _current_ window without closing and re-opening it. – PerlDuck May 24 '19 at 09:39
  • 1
    Good to know thanks a lot! :D I'll let this question open if any others people have anything to suggest, and then I'll accept your answer in few days – Ced May 24 '19 at 09:47
  • 1
    @Ced Glad to hear. You are welcome. Simply spoken: whenever you open a new terminal window, the command `source ~/.bashrc` is automatically run and all definitions and/or commands in that file are executed. The manual call of `source...` is only needed if you want to re-run the definitions and/or commands from `~/.bashrc` without opening a new window. – PerlDuck May 24 '19 at 09:57
1

A simple open function to open file/url (multiple) and the new application disown to the terminal

#!/bin/bash
function openn() {
  if [ "$#" -lt 1 ]; then
    echo "You must enter 1 or more command line arguments";
  elif [ "$#" -eq 1 ]; then
    xdg-open "$1" > /dev/null & disown;
  else
    for file in "$@"; do
      xdg-open "$file" > /dev/null & disown;
    done
  fi
}
Hoang Do
  • 9
  • 2