[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] Expansions in associative array subscripts
From: |
David |
Subject: |
[Help-bash] Expansions in associative array subscripts |
Date: |
Wed, 7 Aug 2019 11:07:47 +1000 |
Hello
Mostly my questions are answered by lurking, which is enjoyable and
educational without troubling anyone, but today I have an actual
question about the bash shell.
I don't feel confident about what context the subscript of an *associative*
array is evaluated/parsed in.
Below I ask a specific question, but for general context I am uncertain
about things like:
"${associativeArray[key]}"
"${associativeArray[$key]}"
"${associativeArray[?]}"
"${associativeArray[*]}"
because I am unclear whether I should also use additional quoting inside
the [subscript], and if so then how.
So the general questions are:
What are the dangers from unexpected expansions?
Does the subscript need to be quoted?
Does it need to be quoted separately from any outer context?
I have read 'man bash' under the heading 'Arrays' and I have read
http://mywiki.wooledge.org/BashGuide/Arrays
under the heading 'Associative Arrays'.
>From both of those references, I understand that the subscript of an
*index* array is evaluated in *arithmetic* context. So an *index* array
subscript has a special context, and I understand that fine.
But it causes me to worry that an *associative* array subscript might have
special context.
I understand the example on the wiki about the difference between
associativeArray[key] and associativeArray[$key].
But, we are generally advised to QEFE, so shouldn't that be
associativeArray["$key"] ?
But how could we nest those quotes inside, while simultaneously quoting
the outer array expansion
"${associativeArray[$key]}" ?
Or do the outer quotes there do both jobs?
Which then leads me to my specific question...
This sentence from 'man bash' (it appears under the heading 'Arrays', in
the paragraph about 'unset') makes me very uncomfortable ...
"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.
Could someone please explain what that sentence means because I have no
idea. Does it mean that pathname expansion might happen inside an
associative array subscript?
I think it would help me to to see an example that illustrates what that
sentence refers to "an unwanted side effect caused by pathname expansion".
Because I am afraid that it warns me something about array subscripts, but
I don't understand *what*.
Thanks for any clarification.
- [Help-bash] Expansions in associative array subscripts,
David <=