Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH?] Nofork and removing newlines



On 3/7/24, Lawrence Velázquez <larryv@xxxxxxx> wrote:
> On Wed, Mar 6, 2024, at 5:22 PM, Mikael Magnusson wrote:
>> "${ foo}" and ${ foo} having the same wordsplitting behavior but only
>> differing in stripping newlines feels a bit magical and weird.
>
> I agree.  Personally, I'm always surprised when quoting does anything
> other than suppress splitting, globbing, and special characters in
> patterns.  For instance, I can never remember this pitfall mentioned
> in workers/52666, even though (I think) I understand why it happens:
>
> 	% print ${:-{}x}
> 	{}x
> 	% print "${:-{}x}"
> 	{x}

This is not really an effect of quoting per se, really it's just luck
that the unquoted form works. You'll notice that if you try print
"${:-}x}" without the quotes it will simply fail. Your example only
happens to pass the parsing stage because the braces are balanced
which they have no inherent reason to do in what is supposedly a
string literal. Because the parser "knows" about the balanced braces
in the unquoted case, it skips past the first } for closing the ${,
but in the quoted form the { is not special in any way, so the first }
does match the ${, and then the second } is just a literal } which is
then printed after the x.

The correct way to write it in both cases would be:
% print ${:-\{\}x}
{}x
% print "${:-{\}x}"
{}x

(you can \escape the { inside the quotes too if you want, but it has
no effect on the result).

-- 
Mikael Magnusson




Messages sorted by: Reverse Date, Date, Thread, Author