Getting the current path in .desktop EXEC command

According to the Desktop Entry Specification:

Field codes must not be used inside a quoted argument

Consequently, your %k is given literally to the bash command. Changing the Exec line to the following avoids doing so:

Exec=bash -c '"$(dirname "$1")"/run.sh' dummy %k

The above works locally, and also works if there's a space in the path. dummy is given to the bash script as its $0 (what it thinks the script name is), and %k's expansion is available as $1. The nested layers of quoting are necessary to conform to the specification and be space-safe.

Note that %k doesn't necessarily expand to a local file path — it can be a vfolder URI, or empty, and a really portable script should account for those cases too. %k also isn't universally supported itself, so you will need to have some constraints on the systems you expect to use it on anyway.


As far as debugging goes, you can use ordinary shell redirection under KDE:

Exec=bash -c ... > /tmp/desktop.log

This isn't standardised behaviour, and it doesn't work under GNOME and likely others. You can also give an absolute path to a script crafted to log its arguments and actions in the way you need.