Icon path in .desktop file

On my desktop I have a .desktop file. If I type:

Icon=/home/ianbell/Pictures/myLogo.png

the icon of the .desktop file changes (the expected result).

But, if I type:

Icon=~/Pictures/myLogo.png

it doesn't work.

Isn't ~/ a shortcut for /home/$USER?


The use of paths in a .desktop file

In a .desktop file, you need to use absolute and full paths. Therefore ~ is not expanded.

This is a commonly made mistake :)

Exceptions concerning icons are a.o. described here:

Icon to display in file manager, menus, etc. If the name is an absolute path, the given file will be used. If the name is not an absolute path, the algorithm described in the Icon Theme Specification will be used to locate the icon.

and here:

Icon field is the icon that should be used by the launcher and represents the application. All icons that are under the directory /usr/share/pixmaps don't need to have their full path specified, but their filename without the extension. For example, if the icon file is /usr/share/pixmaps/wallch.png, then the Icon field should be just 'wallch'. All other icons should have their full path specified.

More information

In a .desktop file:

In the Icon= line, you are allowed to use spaces:

Icon=/home/jacob/Thema/icon/some folder/some icon.png

is fine.

However

In the Exec= line, you are not allowed to use spaces, unless in case of an argument. In all other cases, you need to quote the path steps with a space:

Exec=/home/jacob/Bureaublad/some folder/application

will fail, while

Exec="/home/jacob/Bureaublad/some folder/application"

or

Exec=/home/jacob/Bureaublad/"some folder"/application

will work fine


In short, .desktop file's Icon= path understands absolute paths, but not tilde expansion, because of the specification which defines how .desktop files are supposed to work.

Tilde expansion

  • Where you would see tilde (~) expansion to the $HOME environment variable is often for example in bash, which is the usual login shell you interact with on the command prompt
  • bash, and other POSIX-compliant shells, do tilde-to-$HOME expansion consistent with POSIX specification for shells

.desktop files

  • However, a .desktop file is not the same thing as a shell, it is a plain text configuration file so it would not necessarily work the same way, even if .desktop files and shells can both be found on Linux
  • how a .desktop's file is supposed to work, is defined in Freedesktop's Desktop Entry Specification

Desktop Entry Specification, regarding Icons, says:

If the name is an absolute path, the given file will be used.

This is why you were able to use absolute paths, since it is covered by the spec

If the name is not an absolute path, the algorithm described in the Icon Theme Specification will be used to locate the icon.

When we check the Icon Theme Specification, there is nothing that says it has to follow POSIX's tilde expansion at all.

Summary

Thus, due to the Freedesktop specifications regarding .desktop's Icon= setting, absolute paths are supported, but as you found, POSIX tilde-to-$HOME expansions are not.