How do I write a command for the Exec key in a .desktop file containing a reserved character correctly?

I’m trying to make a .desktop file for Minecraft. Nothing appears to happen upon executing the file. I’ve tried assigning the Exec key as follows:

Exec= java -jar "~/.minecraft/Minecraft.jar"

Exec= java -jar "$HOME/.minecraft/Minecraft.jar"

But I’m not sure how to put in the reserved characters (~ and $) correctly. According to Freedesktop’s Desktop Entry Specification:

If an argument contains a reserved character the argument must be quoted.

and

Quoting must be done by enclosing the argument between double quotes and escaping the double quote character, backtick character (“`”), dollar sign (“$”) and backslash character (“”) by preceding it with an additional backslash character. Implementations must undo quoting before expanding field codes and before passing the argument to the executable program.

But that’s very confusing to me.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It seems a common workaround to execute sh, which will resolve the special symbols and variables correctly:

Exec=sh -c "java -jar ~/.minecraft/Minecraft.jar"

Method 2

In your .desktop file write:

Exec=sh -c 'java -jar "$HOME"/.minecraft/Minecraft.jar'

Sample 2:

Exec=sh -c "u=%u; k=%k; c=%c;"' kd="$k"; [ -f "$kd" ] && kd=`dirname "$k"`; cd "$kd"; uxterm -bg blue -fg cyan3 -fn 9x15'

Explanation:

  • All terms like double-quote, quoting, meta-chars (like $ and ~) are always
    s h e l l – r e l a t e d – the shell (command-interpreter like bash or dash) is (by the way) independent from any terminal (like xterm).
  • When you want the tilde or the shell-var $HOME to be EXPANDED to sth. like /home/myloginname this can only be done by /bin/bash or /bin/sh (==/bin/dash).
  • The xdg-desktop-interpreter’s Exec-line needs a real program (path-to-a-bin-file) and will never expand any shell-stuff directly, but by providing the sh-command with the option -c ‘command-string; cmd… ; cmd…’ all should work fine.
  • %u is a “xdg-placeholder” and is changed into DnD-URL independently (before the shell-command-line is executed)
  • Note that you can comment out lines in a .desktop file with # …

P.S.:

! case   coproc   do  done elif else esac fi for function if in select
then until while { } time [[ ]]

are all “reserved words” (don’t confuse this with the shell’s built-in-cmds), but ~ and $ are meta-chars (the tilde must be un-quoted to be expanded, while $MYVAR may be quoted, if it contain SPACES inside a path)

kind regards
EOF


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x