help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: $${a,b} brace expansion flaw.


From: Kaz Kylheku
Subject: Re: $${a,b} brace expansion flaw.
Date: Sat, 20 Jan 2024 08:33:22 -0800
User-agent: Roundcube Webmail/1.4.15

On 2024-01-20 00:18, Lawrence Velázquez wrote:
> On Sat, Jan 20, 2024, at 2:43 AM, Kaz Kylheku wrote:
>> Someone just posted this finding on Stackoverflow:
>>
>> https://stackoverflow.com/questions/77850130/bash-parse-of-x-y
>>
>> When brace expansion is adjacent to $$, it doesn't expand.
>>
>> Try
>>
>>    echo $${a,b}
>>    echo $$-{a,b}
>>
>> Documented somewhere? Or bug?
> 
> The manual says:
> 
>       To avoid conflicts with parameter expansion, the string
>       `${' is not considered eligible for brace expansion, and
>       inhibits brace expansion until the closing `}'.
> 
> https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html

Yes, that is obvious and explains why we don't get expansion (and don't
want it) in cases like:

  echo ${a,b}

this looks like a ${a...} parameter expansion with a typo, and not brace
expansion.

Note that if we escape the special meaning of $ it works:

  $ echo \${a,b}
  $a $b

When $ is part of the $$ syntax as the second $, that should be enough
to make it clear that it's not the start of a parameter expansion,
the same as \$ makes it clear that that $ isn't a parameter expansion.

Here is what I suspect is the issue. I see in the man page (which I
usually look at first, before the main manual):

"Brace expansion is performed before any other expansions, and any
characters special  to  other  expansions are  preserved in the
result.  It is strictly textual.  Bash does not apply any
syntactic interpretation to the context of the expansion or the
text between the braces."

Brace expansion's handling of $ clearly preserves \$ escaping,
and also considers the presence of the escape in deciding whether
${ should be brace-expanded or not. But it neglects the $$ case,
which seems like it could be given a similar treatment; i.e.
as if the second $ were escaped.

(Or maybe it was considered and omitted for some good reasons;
I'd have to look at the code: comments, anything in its history
or other details.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]