[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: backquote peculiarities
From: |
astian |
Subject: |
Re: backquote peculiarities |
Date: |
Tue, 16 Jul 2019 19:03:00 +0000 |
Chet Ramey:
> On 7/15/19 6:19 PM, astian wrote:
>
>>> I doubt it makes any difference to the timing, which I think
>>> Chet has already answered, but it is worth pointing out that these
>>> two commands ...
>>>
>>> printf '%s\n' "`printf %s "$i"`"
>>> printf '%s\n' "$(printf %s "$i")"
>>>
>>> which (I believe)) are supposed to be the same thing, using the
>>> different (ancient, and modern) forms of command substitution aren't
>>> actually the same. In the first $i is unquoted, in the second it is
>>> quoted. Here, since its value is just a number and IFS isn't being
>>> fiddled, there is not likely to be any effect, but if you really
>>> want to make those two the same, the first needs to be written as
>>>
>>> printf '%s\n' "`printf %s \"$i\"`"
>>>
>>> Such are the joys of `` command substitutions (just avoid them).
>>>
>>> kre
>>
>> Dear Robert Elz, I'm aware of several of its peculiarities and I typically do
>> avoid them. However, is it true that $i is unquoted in the first case?
>
> POSIX makes it undefined behavior, and different shells do it differently.
> Bash makes the $i quoted within the `` string, as you discovered.
Ah, thanks for the clarification. I wonder if the excerpt below (particularly
the last sentence) would be the relevant wording of POSIX, i.e. that some
shells might interpret the command as the concatenation of a double-quoted
string, the unquoted $i, and another double-quoted string:
"`printf %s "$i"`" => CONCAT("`printf %s ", $i, "`")
Which would then lead to "undefined results".
Cheers.
---
2.2.3 Double-Quotes [0]
Enclosing characters in double-quotes ( "" ) shall preserve the literal value
of all characters within the double-quotes, with the exception of the
characters backquote, <dollar-sign>, and <backslash>, as follows:
[...]
`
The backquote shall retain its special meaning introducing the other
form of command substitution (see Command Substitution). [...] Either of
the following cases produces undefined results:
- A single-quoted or double-quoted string that begins, but does not
end, within the "`...`" sequence
- A "`...`" sequence that begins, but does not end, within the same
double-quoted string
[...]
0:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_03