How do I access an environment variable in a .desktop file's exec line?
Asked Answered
G

1

30

My program requires an environment variable as part of one of its parameters:

myprogram --folder=$HOME/.special

However, if I put this into a .desktop file's exec line, it doesn't work:

Exec=myprogram --folder=$HOME/.special

The $HOME seems to resolve to nothing.

Galvano answered 24/1, 2012 at 0:14 Comment(0)
G
45

By default environment variables do not seem to be resolved by all implementations, however you can instead exec sh, which will resolve the passed environment variable. Note that the desktop spec also requires you to escape the = and $ character with a backslash. So you want:

Exec=sh -c "myprogram --folder\=\$HOME/.special"

For the full list of characters that need escaping, see the specification

Galvano answered 24/1, 2012 at 0:21 Comment(8)
When your characters are in the string, you do not need to escape any characters.Niklaus
I need to try this trick, but I love it. Here is an IMO slightly more readable version of the spec, developer.gnome.org/desktop-entry-spec/#exec-variables .Giarla
This worked perfectly for my use-case, which was predeclaring a variable before the command was run: Exec=sh -c "FOO='bar' /path/to/exec"Immanuel
Nice work! I prefer using bash instead of sh, but it's pretty much the same thing. Here's the bash version: Exec=bash -c "myprogram --folder=$HOME/.special".Swinge
You could use exec to avoid having an additional shell process running: Exec=sh -c "exec myprogram --folder=$HOME/.special".Fianna
It works for Exec, but it doesn't work for values Path or Icon.Ryon
What id you already have an Exec for the .desktop program like Exec=/home/coyote/anaconda3/bin/spyder %F but then I want also Exec=sh -c "LD_LIBRARY_PATH\=/usr/local/lib/:\$BOOST/lib:\$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/hdf5/serial"Preston
More definitive description of the Exec key: specifications.freedesktop.org/desktop-entry-spec/latest/…Disintegration

© 2022 - 2024 — McMap. All rights reserved.