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: Paxsali
Subject: Re: $${a,b} brace expansion flaw.
Date: Sat, 20 Jan 2024 20:55:51 +0100

Hello Kaz,

first of all I'd like to say that saying I was relying on Chat AI and
having low experience on this area are personal insults.

I think anyone who repeatedly replies in such tone should be banned from
this mailing list.

Secondly, yes I had accidentally forgotten to reply to help-bash when I
replied to you.

My original reply to you (for the record of others) was:

Hi Kaz,
> the braces expression will not be evaluated, because it's not a separate
> argument (not a space delimited argument to echo). If you need to print
> both expressions with no spaces separated, you have to evaluate then
> separately and print them united.
> Echo is not suitable for this job. What you want is to look at printf.
> declare -i pid
> declare -a list
> pid=$$
> list+=({a,b})
> printf '%d%s\n' "${pid}" "${list[*]}"
> pid is an integer, list is an indexed array.
> Notice how in the printf format string there is no space between the two
> placeholders and how the list is evaluated as a string ("[*]"). This is so
> the number of arguments to printf match the number of placeholders in the
> format string.
> Hope that helps.


I think my reply was reasonable, given the amount of information you
provided, which - you didn't clearly say what your expected
output should be to begin with.

Maybe, before we go any further at all, you could describe what you mean by:

echo ${a,b}

I have the latest bash reference manual in front of me and I cannot find
this form of brace or shell parameter expasion.

Sources:
- https://www.gnu.org/software/bash/manual/bash.html#Brace-Expansion
-
https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion

If you want the output to be "$a $b", you have to escape the dollar sign,
because the form ${name} is just another convention for $name.

And the comma means lower case the first character, two commas mean
lowercase the whole string.

Example:

$ FOO=BAR; echo ${FOO,}
bAR

$ FOO=BAR; echo ${FOO,,}
bar

If you use extra characters after the comma inside a shell parameter
expansion, it just returns the variable's value:

$ FOO=BAR; echo ${FOO,b}
BAR

$ FOO=BAR; echo ${FOO,,b}
BAR

So, now from the four different expansion forms which are possible...:

- $$    for PID
- $variable, ${variable}  regular variable evaluation
- {a,b}  for brace expansion
- ${name,[,]}  for lower case string manipulation

...what exactly is your problem and what exactly do you want to achieve
(like, what output do you want)?

I will report you, if you insult me again.

On Sat, Jan 20, 2024 at 8:08 PM Kaz Kylheku <kaz@kylheku.com> wrote:

> On 2024-01-20 02:54, Paxsali wrote:
>
> > Hi Kaz,
> >
> > the braces expression will not be evaluated, because it's not a separate
> argument (not a space delimited argument to echo).
>
> This is completely false. Brace expansion does not have to comprise
> an entire argument. For instance a{x,y}b generates axb and ayb.
> It is a critically important feature of brace expansion that it
> combines with, and repeats, adjacent material in the same field,
> and that multiple occurrences of the notation can occur in
> a single field as in img{001,002}.{jpg,png}, and that occurrences
> of the notation can also nest: {a,x{b,c}}.
>
> Your examples don't even contain any mention of the $${a,b}
> issue at all; your reply doesn't speak to that.
>
> It is transparent to me that you have low experience in this area
> and are likely relying chat AI, whose output you are not able to
> check for correctness and pertinence.
>
> Good thing you had the presence of mind not to post this to the
> mailing list with "reply all".
>


reply via email to

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