Hi davidson,

I share your great answer (see below) with the whole list as I think your 
explanations are very didactic, relevant and could be beneficial to anyone.

 I've just tested and it works so thank you very much :)

If I have well understood, my issue was that I've mixed up reserved characters 
with distinguished ones, right?

So if I sum up, here is the logic:

* As soon as there is at least one *reserved* characters (like [SPACE] [TAB] 
[NEWLINE] " ' \ > < ~ | & ; $ * ? # ( )) in an argument, one needs to escape 
the whole argument

* Argument escaping is done by adding double quotes around it and prefixing 
each *distinguished* characters (like " ` $ \) with a \

* Reserved characters must be escaped individually only if they must lose their 
specific meaning and be interpreted litterally

* Escaping reserved characters is done by prefixing them with a \ but as the 
argument needs quoting, the extra \ itself must be prefixed by a leading \ so a 
litteral $ becomes \\$, a \ becomes \\\\, & becomes \\& and so on...

Am I correct please?

Thanks a lot!
Best regards,
l0f4r0


16 juin 2020 à 07:07 de david...@freevolt.org:

> On Mon, 1 Jun 2020, l0f...@tuta.io wrote to debian-user:
>
>> Let's take an example, here is my fusuma.desktop file:[Desktop Entry]
>>
> [...]
>
>> Exec=sh -c 'if which ruby >/dev/null && which gem >/dev/null; then $(ruby -r 
>> rubygems -e "puts Gem.user_dir")/bin/fusuma -d; fi'
>>
>
> The third argument (everything after the '-c') of this Exec value,
> your shell script, contains all kinds of characters the spec indicates
> are reserved[1], like the following:
>
>  [SPACE] > & ; $ ( " )
>
> This means that the spec requires it (the argument containing reserved
> characters) to be quoted[2].
>
> But how to quote an argument? The spec explains[3] this is done by
> enclosing the argument inside double quotes, and escaping all of its
> occurences of four especially distinguished characters
>
>  " ` $ \
>
> with a backslash prefix.
>
> So to quote the argument (the script), remove the single quotes
> surrounding the script, replace them with double quotes, and then
> inside the script add one backslash in front of each occurence of the
> especially distinguished characters which require escaping (just the
> dollar sign introducing command substitution, and the pair of double
> quotes inside that command substitution) to obtain:
>
>  Exec=sh -c "if which ruby >/dev/null && which gem >/dev/null; then \$(ruby 
> -r rubygems -e \"puts Gem.user_dir\")/bin/fusuma -d; fi"
>
>>> From my understanding, I've tried:
>>>
>> Exec=sh -c "if which ruby \>/dev/null \&\& which gem \>/dev/null\; then 
>> \$\(ruby -r rubygems -e \"puts Gem.user_dir\"\)/bin/fusuma -d\; fi"
>>
>> I get no warning except it does not work now!
>>
>> It's probably a matter of successive backslashes but I don't
>> understand how many are needed exactly...
>>
>> Of course I'm interested in the correct syntax for my example but
>> it's not so critical as it's already working.
>>
>
> In your attempted revision, you seem to have selectively prefixed
> backslashes to some reserved characters (eg, '>') but not others (eg,
> ' ').
>
> But why prefix backslashes to any characters at all, beyond those four
> required by the spec (the ones I called "especially distinguished")?
>
>  " ` $ \
>
> Reserved characters in an argument don't generally need any escaping
> at all.[4] Their *presence* means an argument containing them must be
> quoted, and quoting an argument entails prefixing a backslash to the
> four *special* reserved characters distinguished above.
>
> QUOTES (from the spec excerpt in OP) and NOTES
>
> 1.[quote] Reserved characters are space (" "), tab, newline, double quote,
> single quote ("'"), backslash character ("\"), greater-than sign
> (">"), less-than sign ("<"), tilde ("~"), vertical bar ("|"),
> ampersand ("&"), semicolon (";"), dollar sign ("$"), asterisk ("*"),
> question mark ("?"), hash mark ("#"), parenthesis ("(") and (")") and
> backtick character ("`").
>
> 2.[quote] Arguments may be quoted in whole. If an argument contains a
> reserved character the argument must be quoted.
>
> 3.[quote] 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.
>
> 4.[note] Unless you require a reserved character's literal
> interpretation. Your given example is simple because you want the
> non-literal interpretations of each of its reserved characters.
>

Reply via email to