15

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?

4
  • 1
    ~ is short for your home directory only in a shell.
    – user253751
    Jan 17, 2016 at 21:09
  • @JacobVlijm: sorry, I fell asleep, I'm programming night and day :-)
    – user492815
    Jan 19, 2016 at 7:15
  • 1
    @IanBell Haha I know the feeling :) Jan 19, 2016 at 7:28
  • /home/$USER won't work. It seems you are looking for - /home/$(whoami). See my comments below.
    – Ajay Kumar
    Mar 17, 2019 at 15:32

2 Answers 2

16

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

3
  • So how do I reference a path relative to "the users's home directory"? Aug 4, 2018 at 5:12
  • Aaron Franke, you can use $(whoami) to get current username. I havent tried but this should work - for home dir for current user - /home/$(whoami).
    – Ajay Kumar
    Mar 17, 2019 at 15:29
  • I tried and it works Aaron.
    – Ajay Kumar
    Mar 17, 2019 at 15:30
5

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.

1
  • plasmashell expands ~ in Exec just fine. Sep 11, 2022 at 11:03

You must log in to answer this question.