help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Expansions in associative array subscripts


From: David
Subject: Re: [Help-bash] Expansions in associative array subscripts
Date: Thu, 8 Aug 2019 01:34:45 +1000

On Wed, 7 Aug 2019 at 22:49, Greg Wooledge <address@hidden> wrote:
> On Wed, Aug 07, 2019 at 11:07:47AM +1000, David wrote:

> > Below I ask a specific question, but for general context I am uncertain
> > about things like:

[...snip...]

Thanks to *everyone* for your effort to write replies and share your knowledge
to help me!

> > Does the subscript need to be quoted?
>
> Only if it's a literal string that needs quoting due to metacharacters
> inside it. E.g. aa["my]key"] needs quotes because of the ] character.
>
> > Does it need to be quoted separately from any outer context?
>
> If applicable, yes. "${aa["my]key"]}"
>
> > But, we are generally advised to QEFE, so shouldn't that be
> > associativeArray["$key"] ?
>
> If you like.
>
> > But how could we nest those quotes inside, while simultaneously quoting
> > the outer array expansion
> > "${associativeArray[$key]}" ?
>
> As I showed earlier. Exactly the same as "${foo/bar/"$baz"}" or
> "$(cmd1 "$arg1")" where quotes nest because of syntax in between the
> sets of quotes.

Wow, thank you for that sentence, that right there is what clarified this
for me. I was completely unaware that the double quotes might nest.
So with that new understanding, my other related questions fade away.

That's why my original question was attempting to talk about "context"
that the array subscript is evaluated/parsed in (but it's hard to talk clearly
about things one does not properly understand). And that's the key to
the answer! I was not aware that there is something in the syntax that
creates a "context" where double quotes can nest.

I am familiar with that occurring with "$(echo "foo")" for example as Greg
showed. But I didn't notice anywhere in the manual that this is explained
for array subscripts. Where did you learn about that? Please point me to
other documentation of that if it exists.

In the absence of documentation I assume that it is the combination
of declare -A plus the brackets [] that create the new context,
and that what is inside there is expanded as a new "word"? Is there an
agreed correct terminology for that handling of the associative array
brackets?

[...snip...]

> > "Care must be taken to avoid unwanted side effects caused by pathname
> > expansion."
> >
> > I cannot understand the scope of that sentence, or its relationship to the
> > sentences that surround it.
>
> That sentence doesn't appear in the 5.0 man page. It appears in the 4.4
> man page, though.

Sorry that I created extra work for you by forgetting to mention my
bash version.

> The warning is about cases like this:
>
> $ touch a1
> $ a=(zero one two)
> $ unset a[1]
> $ declare -p a
> declare -a a=([0]="zero" [1]="one" [2]="two")
>
> In that example, a[1] is globbed to a1 because of the file named a1 in
> the current working directory. So the resulting command is unset a1,
> which is very different from unset 'a[1]'.
>
> $ unset 'a[1]'
> $ declare -p a
> declare -a a=([0]="zero" [2]="two")

Thank you for that example! I see now that it's not so much about the
[subscript], but rather about the ambiguous use of the brackets [].



reply via email to

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