How does Linux recognize and process .desktop files?
A Desktop entry (.desktop file) is an executable plain text file, which lacks a shebang. Its contents typically look like:
[Desktop Entry]
Name=Fancy Example
Exec=/usr/bin/example --enable-fancy
(with more possible lines after that). This is clearly not valid as a shell script:
$ [Desktop Entry]
[Desktop: command not found
However, actual shell scripts don't need to have a shebang:
$ cat > test.sh
echo foo
$ chmod +x test.sh
$ ./test.sh
foo
How does my system know that the desktop entry is indeed a desktop entry, and feed it to the appropriate interpreter, rather than trying to interpret it as a shell script?
... and what exactly is "the appropriate interpreter"?
1 answer
The following users marked this post as Works for me:
| User | Comment | Date |
|---|---|---|
| Karl Knechtel | (no comment) | Jun 25, 2025 at 17:25 |
Upon further investigation, .desktop files don't actually work by executing them. Attemping to run them in a terminal will fail as expected:
./fancy.desktop: line 1: [Desktop: command not found
./fancy.desktop: line 2: Example: command not found
./fancy.desktop: line 3: --enable-fancy: command not found
It appears that Nemo (ab)uses the executable permissions bit on these files as a "trusted" marker (since they after all represent an arbitrary command to run). When you create a "launcher" using the GUI, it sets chmod +x on the resulting file; if you chmod -x and then try to double-click the launcher, Nemo will pop up a warning that the launcher is "untrusted" and offer the option to run it anyway or to mark it as "trusted" (which just sets the permission again).
But the file isn't going through any kind of command processor or being recognized by a file magic number (like the #! bytes of a shebang line); the logic is implemented by Nemo, not by any internal Linux mechanism (such as binfmt_misc). I suppose it would be possible to implement a command-line .desktop file processor and register it with binfmt_misc, but it's hard to imagine a use case.

0 comment threads