7

I can add env in .desktop file in /usr/share/applications/

Exec=env FOO=bar /usr/bin/my_prog

but I need to set 2 environment variables and no approach that I tried works (using env twice, appending second assignment after ;)

How can I set 2 env variables in .desktop file ?

5
  • 6
    The documentation for env says that name=value can be repeated. Did you try Exec=env FOO=bar SNA=fu /usr/bin/myprog ? Commented Jul 29, 2018 at 12:10
  • @Mark Plotnick - yes I have tried that. but only the first variable has effect. The second is ignored Commented Jul 29, 2018 at 12:15
  • 3
    Cannot reproduce this and for me it works with Exec=env foo=bar bar=foo /home/user/test/test.sh. Where test.sh echos the two variables foo and bar. You might want to write a wrapper script as an alternative and call that wrapper script in your .desktop entry.
    – Thomas
    Commented Jul 29, 2018 at 12:27
  • 1
    Do your environment variable values contain any blanks or other non-alphanumeric characters? Commented Jul 29, 2018 at 13:31
  • @MarkPlotnick Besides setting up multiple env vars, I have a special case where my environment variable value itself contains an = sign. How to escape it?
    – Koder101
    Commented Sep 11, 2021 at 6:54

2 Answers 2

5

As mentioned above, your variant with the addition of several variables should work correctly

Exec=env FOO=bar /usr/bin/my_prog

So either there is some kind of external factor, or the question is not fully correct

2
  • Besides setting up multiple env vars, I have a special case where my environment variable value itself contains an = sign. How to escape it?
    – Koder101
    Commented Sep 11, 2021 at 6:54
  • FOO=foo=bar HELLO=hello=world && printf "$FOO $HELLO\n" working fine in bash, but you can also quote values for safety
    – lucidyan
    Commented Sep 12, 2021 at 21:56
1

This question keeps getting asked and answered because the answers uniformly don't work, including the selected answer above.

Here are working examples from my customized .desktop files

Exec=bash -ic "export PATH=$PATH:/opt ; /opt/qxmledit/qxmledit %u"

In this case, qxmledit couldn't find its libraries because the package was installed in /opt. I added /opt last, just in case it finally gets packaged by Debian. Setting the PATH fixes this.

Exec=bash -c "export PATH=/usr/lib/jvm/java-8-openjdk-amd64/bin:$PATH ; /home/user/Downloads/docear-1.2.0.0_stable_build291/docear.sh "

In this case, the shell to start docear was having trouble finding the right Java to run. I added the java bin directory first to prevent seeing my preferred java binaries.

The desktop format is very finicky when it comes to setting environment vars. Don't deviate too much from the format. The double quotes go around everything bash is supposed to execute. Also, I am assuming that bash is on your current path.

Also, I used the -i option to load bash as an interactive shell. You may not want this.

If you haven't found any better answer, please link to this one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .