How can I do command substitution in a .desktop file?

Can I do bash-like command substitution in a .desktop launcher? For example, something analogous to this:

Exec=/some/prog --arg "$(zenity --entry)"

By that I mean "run this command and place its stdout in the containing command line, then execute that".

Is there a way to do it without making a separate bash script file? I found this hack (running bash -c '...') that might work, but maybe there's a proper way to do it, like some .desktop special syntax?

EDIT: I just found out that the $(...) works as normal inside the .desktop file ... almost.

I still have the problem of making a failure (nonzero exit code) in the substitution command abort the enclosing command. How can I do that? I tried assingnig a variable like:

Exec= resp="$(zenity --entry)" && /some/prog --arg $resp

But it didn't work, because instead of aborting, it tries to execute the "&&" as a command when the inner command fails.

1 Answer

I ended up having to use bash -c.

This is the way to properly handle command substitution with fail support, while passing the files in %F correctly. I have to expand %F outside the '...' quotes and pass the files as arguments, then expand them again with "$@" inside the single quotes, otherwise I can't properly handle multiple files with spaces on each path.

Exec= bash -c 'r=$(zenity --entry) && /some/prog --arg "$r" "$@"' "$0" %F
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like