Alter Behavior of xdg-open

6.1k views Asked by At

I use the command xdg-open quite a lot in my Ubuntu Linux terminal. However, two things irk me:

  1. Is it possible to suppress the error messages?

  2. Is it possible to get the command to always complete? (That is, not continue running, so that I have another "new line" in my terminal).

I realize 2 may not be possible, because of the way the program works, but I imagine 1 is.

3

There are 3 answers

0
Flamma On BEST ANSWER

First one is easy. Just

alias xdg-open="xdg-open 2>/dev/null"

If you want it permanently, just add that line to ~/.bashrc file.

I recommend you to think twice if you want to become blind to errors, though.

The second one is quite confusing to me. xdg-open shouldn't be intereactive. In my computer (Debian sid) xdg-open execs the command and ends, even if the command itself has not ended (ie: you have not closed the application opened for the URL). I think this should be the behaviour of xdg-open on any platform (it's supposed to work exactly the same way on any XDG system, that's its very purpose).

Anyway, for any command you launch in a shell, if you want it to be non-interactive, that is, to allow to enter commands even if the previous one hasn't finished, you just attach "&" to the end of it. Example:

# prompt is not shown until you close the calculator
$ gnome-calculator
# prompt is shown right after opening calculator and you can 
# work on the shell even if you don't close it
$ gnome-calculator &
0
Denny On

I maybe late for the answer, but I got exactly the same problem like you have / had.

I tried to start a URL with xdg-open, my default browser is firefox, and not xdg-open but firefox started with an error:

[user@user-pc ~]$ xdg-open https://www.google.de # the page opens fine, but firefox had an error
[user@user-pc ~]$ 
(process:3783): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed

# needed to press enter here

xdg-open closed fine but the firefox error stayed and I need to press enter to get the bash moving.


To get along this problem I called xdg-open within a new shell putting those output to /dev/null:

bash -c "xdg-open https://www.google.de" 2> /dev/null

The page opened fine, no error shown – rather nothing has been shown. And no need to press enter.

0
dimo414 On

I know this is an old question, but I want to clarify the accepted answer and address question #2.

  1. xdg-open prints to both stdout and stderr in different circumstances. Notably, if Chrome is your default browser and you have a window open it prints Opening in existing browser session. to stdout and to my knowledge there's no way to disable this (no --quiet or similar). So to suppress this you need xdg-open URL >/dev/null (not 2>/dev/null). I also observed that with Firefox on a fresh Ubuntu install it prints several error messages to stderr, but those seem to reflect an actual issue with how Firefox is configured (I didn't investigate this further, I just installed Chrome....).

  2. The command does actually complete! It's just a bit confusing to look at because it starts an asynchronous subprocess and that process is what generates this output, after the shell has already returned to you. You can observe this by trying xdg-open URL; sleep 1 in your shell - the output will print before the sleep completes and it will look more normal.

    The easiest way to work around this (aside from discarding the xdg-open output) is to just hit enter in your shell - that will print a new prompt line for you and will make things look normal again.