2

I'm trying to create a .desktop file that runs a shell script that's in the same folder as the .desktop file. So far I got the following to work:

Exec=bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'

There's just two issues with that command. The first issue is that desktop-file-validate returns a bunch of errors:

start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a reserved character ''' outside of a quote
start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a non-escaped character '$' in a quote, but it should be escaped with two backslashes ("\\$")
start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a reserved character '&' outside of a quote
start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a reserved character '&' outside of a quote
start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a reserved character '&' outside of a quote
start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a reserved character '&' outside of a quote
start.desktop: error: value "bash -xc 'cd "$(dirname %k)" && bash ./patch.sh && bash -c "read -n1"'" for key "Exec" in group "Desktop Entry" contains a reserved character ''' outside of a quote

and I don't understand any of these. There's single quotes around the entire "bash" parameter, and inside of that, everything's using double quotes. The desktop file works, and the bash command itself (when run outside of the desktop file) also works, so why does this validation tool complain about missing quotations? Why would I need to escape a damn && in a script?

The second issue is that the desktop file doesn't work if the path to it contains a space. I believe that that might be related to all the errors the validator gives me.

But I have absolutely no idea how to properly escape all that so that the errors go away. It just doesn't make sense to escape an && that's inside single quotes already, does it?

2 Answers 2

1

I don't understand any of these

If we read the error carefully we can reduce it to:

error: value "bash -xc '...'" contains a reserved character ' outside of a quote

It's telling us that we can't use ' without quoting it first. One option might be to double-quote everything. The internal double-quotes might need to be escaped in that case. The other option is to escape the single quote so that it is treated as part of the line and not a special operator.

I suspect one of these will work:

Exec=bash -xc \'cd \"\$(dirname %k)\" \&\& bash ./patch.sh \&\& bash -c \"read -n1\"\'
Exec="bash -xc 'cd \"$(dirname %k)\" \&& bash ./patch.sh && bash -c \"read -n1\"'"

Why would I need to escape a damn && in a script?

&& is a bash operator. Whatever is parsing this line isn't bash. Instead, it's probably just taking the line, pre-processing it (scanning for anything specific that the user might want this application to know), tokenizing the remainder, then launching a process with the first token, and passing the rest as arguments. It probably has its own special rules and syntax.

1
  • Thanks for your response, but neither command works properly. With the first one the terminal window immediately closes again (same effect as when there would be a space in the path), and with the second one I get an error message, like "There was an error starting this application". Nov 13, 2020 at 5:34
1

See the specification. It is complicated. You must quote arguments that have reserved characters (a long list) but only with double-quotes. So your command is effectively currently passing 'cd as the 2nd argument to bash. Also && is not inside double-quotes, and so it generates an error.

I don't use gnome so cannot test this, but the spec says you must escape characters "`\$ inside the string, (and you must escape % by %% if you don't want to use a field code as you do), so you could try something like

bash -xc "cd \"\\$(dirname %k)\" && bash ./patch.sh && bash -c \"read -n1\""

The spec could do with a few concrete non-trivial examples.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.