Re: [PATCH] shell portability: Use sed instead of non-portable variable expansion
- From
Johannes Sixt <j.sixt@viscovery.net>
- Date
- Sep 5, 2011, 08:16 UTC
- Message-ID
- <4E648546.8060303@viscovery.net>
- In-Reply-To
- <7v39gbxwi6.fsf@alter.siamese.dyndns.org>
Am 9/5/2011 9:45, schrieb Junio C Hamano:
Show 13 quoted lines
> Johannes Sixt <j.sixt@viscovery.net> writes: > >> Actually, it's the opposite: Within double-quotes, a backslash is only >> removed when the next character has a special meaning (essentially $, `, >> ", \), otherwise, it remains and loses its quoting ability. This means, >> that the backslash would remain as a literal character in our patterns on >> the right of % or #, and they would not work anymore as intended. > > That's strange... > > I thought that VAR=<any string without $IFS character in it> would behave > identically to VAR="<the same string as above>". You seem to be saying > that they should act differently.
They are not the same.
First of all, the value of $IFS is irrelevant whether or not you need double-quotes on the RHS of an assignment, because it is purely a syntactic matter; $IFS plays no role during syntax analysis. It is only the presence of white-space that sometimes[*] requires quoting of some form.
The most visible difference is a backslash that is followed by a character that is not special:
$ foo="a\xb" env | grep foo; foo=a\xb env | grep foo foo=a\xb foo=axb
But it is the same elsewhere in a command:
$ echo "a\xb"; echo a\xb a\xb axb
The reason is that a backslash inside double-quotes remains as a literal character when it is not followed by a special character, whereas outside double-quotes an unquoted backslash is always removed.
[*] No quoting is required in cases like this: VAR=$(echo foo)
Show 7 quoted lines
>> [?] instead of \? is certainly also worth a try. > > I obviously agree. Besides, [?] would sidestep the tricky backslash vs > double quote issue entirely, so it would be a more robust solution to > leave it around than "sometimes you need to avoid double-quote and some > other times you would need double-quote" for other people to mimic writing > tests later.
Good point, and I shall prefer this solution as well.
-- Hannes