[BUG] Associative array initial reference name is made available in another context

2023-07-01 Thread Top Dawn
Hello there,

I believe there is a bug with associative arrays, when once referenced in
another function through the -n option, both the new reference name and the
old one are made available.

```bash

#!/bin/bash
function my_function(){
declare -A my_array
my_array=(["one"]="one")
other_function "my_array"
}

function other_function(){
declare -n other_array="${1-}"
echo "${other_array["one"]}"
echo "${my_array["one"]}"
}

my_function

```

will output :

```bash

one
one

```

I believe this to be a bug. I tried to reference the same name through
`declare -n my_array="${1-}"` but then get circular reference errors.

Best,


Re: [BUG] Associative array initial reference name is made available in another context

2023-07-02 Thread Top Dawn
>
> What makes you think so? Variables are always visible in invoked
> functions, unless you shadow them using local/declare/typeset.
>

Thank you very much for this information, I didn't know invoked functions
inherited their parent functions variables.
I understand better now the use of declare -n "reference", to make a copy
of the referenced variable.

Best,


On Sat, Jul 1, 2023 at 10:38 PM Lawrence Velázquez  wrote:

> On Sat, Jul 1, 2023, at 3:55 PM, Top Dawn wrote:
> > I believe there is a bug with associative arrays, when once referenced in
> > another function through the -n option, both the new reference name and
> the
> > old one are made available.
> >
> > ```bash
> >
> > #!/bin/bash
> > function my_function(){
> > declare -A my_array
> > my_array=(["one"]="one")
> > other_function "my_array"
> > }
> >
> > function other_function(){
> > declare -n other_array="${1-}"
> > echo "${other_array["one"]}"
> > echo "${my_array["one"]}"
> > }
> >
> > my_function
> >
> > ```
> >
> > will output :
> >
> > ```bash
> >
> > one
> > one
> >
> > ```
> >
> > I believe this to be a bug.
>
> What makes you think so?  Variables are always visible in invoked
> functions, unless you shadow them using local/declare/typeset.
>
> % cat /tmp/foo.bash; echo
> function my_function(){
> declare -A my_array
> my_array=(["one"]="one")
> other_function
> }
>
> function other_function(){
> echo "${my_array["one"]}"
> }
>
> my_function
>
> % bash /tmp/foo.bash
> one
>
> --
> vq
>


Re: [BUG] Associative array initial reference name is made available in another context

2023-07-03 Thread Top Dawn
>
> This is known as dynamic scoping. There is text in the manual describing
> it.
>
Thank you, this was an interesting read. :)

Best,

On Mon, Jul 3, 2023 at 2:59 PM Chet Ramey  wrote:

> On 7/2/23 9:04 AM, Top Dawn wrote:
> >>
> >> What makes you think so? Variables are always visible in invoked
> >> functions, unless you shadow them using local/declare/typeset.
> >>
> >
> > Thank you very much for this information, I didn't know invoked functions
> > inherited their parent functions variables.
>
> This is known as dynamic scoping. There is text in the manual describing
> it.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
>
>