7

Some people add the following line to a .desktop file:

#!/usr/bin/env xdg-open

But when I checked files under the /usr/share/applications/ in my debian sid, there's no such file that would include this line.

So the question is simple: should it be added or not?

Mikhail Morfikov
  • 10,309
  • 19
  • 69
  • 104
  • 1
    This has to do with [.desktop file security](https://lists.freedesktop.org/archives/xdg/2009-February/010209.html) and how _freedesktop.org_ people thought it should be implemented. For e.g. on all freedesktop compliant DE's that line is _automatically_ added when you _"mark"_ a `.desktop` file in ~/.local/share/applications _"as trusted"_ (e.g [here](https://mail.gnome.org/archives/desktop-devel-list/2009-February/msg00132.html)'s how gnome implemented this ..). So this is not a matter of _"If you don't need it to be executable then you don't need it."_ – don_crissti Jul 10 '16 at 15:48
  • 1
    That should have been an answer, not a comment on the question. – JdeBP Jun 25 '17 at 15:55

1 Answers1

7

By having a #! line then the .desktop file can be made executable and run directly (e.g. from a command line, or some file browsers).

The idea behind wrapping with env is to handle different OS's where xdg-open might be in different directories. For example, on Debian the program is in /usr/bin/xdg-open but on another OS it might be elsewhere. The location of env is pretty much fixed and this construct will effectively result in $PATH being searched for the xdg-open program.

On my Debian install no file in /usr/share/applications is marked executable and none of them have a #! line.

So if you want your .desktop file to be executable then you need a #! and wrapping it with env will help in portability. If you don't need it to be executable then you don't need it.

Stephen Harris
  • 42,369
  • 5
  • 94
  • 123
  • 1
    Many KDE applications have executable `.desktop` files on debian to. You can find them (if installed) with `$ find /usr/share/applications/ -executable -type f` – Jonas Stein Jun 27 '17 at 17:39