You are not logged in.

#1 2020-12-10 16:12:52

solskog
Member
Registered: 2020-09-05
Posts: 407

[Solved] bash 5.1.0(1)-release associative array

Before bash 5.1.0(1)-release this associative array inside a function assigned two elements.

f()
{
    declare -A F=([a]=1 [b]=${F[a]})
    echo ${F[@]}
}
# f
# 1 1

After bash 5.1.0(1)-release, the same function assigns only one element '1'
But the same array outside function gives the correct result.

# declare -A F=([a]=1 [b]=${F[a]})
# echo ${F[@]}
# 1 1

It seems a declare statement can no longer refer to a predefined element within a function definition?

Last edited by solskog (2020-12-11 06:38:31)

Offline

#2 2020-12-10 21:07:28

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [Solved] bash 5.1.0(1)-release associative array

That sounds like a bug was fixed. tongue

I do see to recall something like this on bug-bash.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3 2020-12-10 23:55:03

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [Solved] bash 5.1.0(1)-release associative array

By searching the source code of bash 5.1.0(1)-release. There are many tests for associative array, but couldn't found anything that testing predefined element like this.

declare -A F=([a]=1 [b]=${F[a]})
declare -p F

Where can I locate bug-bash?

Offline

#4 2020-12-11 00:38:37

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [Solved] bash 5.1.0(1)-release associative array

https://lists.gnu.org/archive/html/bug-bash/

EDIT: That being said, are you sure you don't have an old definition of the array from before?

Last edited by eschwartz (2020-12-11 00:43:08)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#5 2020-12-11 01:45:46

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [Solved] bash 5.1.0(1)-release associative array

I have searched bug-bash, help-bash and bash-announce. There are no mentioning of this exact behavior. And it worked in version 5.0.18(1)-release. I haven't locate which commit resulted this behavior yet but I am trying.

eschwartz wrote:

That being said, are you sure you don't have an old definition of the array from before?

No old definitions. This Test is specifically designed to test this behavior, I have many scripts/function depends on this, which failed after upgrading to bash 5.1.0(1)-release.

Offline

#6 2020-12-11 01:56:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [Solved] bash 5.1.0(1)-release associative array

I tried to test this with the following script:

#!/bin/bash

f() {
	declare -A F=([a]=1 [b]=${F[a]})
	echo ${F[@]}
}

f

declare -A G=([a]=1 [b]=${G[a]})
echo ${G[@]}

This is the output of that script right before updating bash:

$ ./script
1 1
1 1

and after the update:

$ ./script
1
1

So with this script, I can confirm that there was a change related to what you are describing, but not as you are describing: whether or not the declaration is in a function or not is irrelevant.  Do you have a different test-case that highlights your concern?

Last edited by Trilby (2020-12-11 01:57:28)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2020-12-11 02:01:59

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [Solved] bash 5.1.0(1)-release associative array

Trilby wrote:

but not as you are describing: whether or not the declaration is in a function or not is irrelevant.

It's relevant in the sense that inside a function, it's unlikely to dirty future results in the same shell.

This dichotomy is why I asked "are you sure you don't have an old definition of the array from before", because it (the OP being incorrect/imprecise in the reported testcase) is the only reason I could think of for the difference.

Last edited by eschwartz (2020-12-11 02:07:33)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#8 2020-12-11 02:06:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [Solved] bash 5.1.0(1)-release associative array

Ah, I mean it's not relevant for the output.  The OP is claiming that with the new version of bash, the code behaves differently inside and outside a function.  My test with the script I provided demonstrates that this is not the case - which is why I was asking for their code that does demonstrate what they claim.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2020-12-11 02:25:59

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [Solved] bash 5.1.0(1)-release associative array

Here comes two test cases with different release.

# cat bug.sh
bug()
{
    declare -A BUG1=([a]=1 [b]=${BUG1[a]})
    declare -p BUG1
}
bug
declare -A BUG2=([a]=1 [b]=${BUG2[a]})
declare -p BUG2
unset BUG2
unset bug

# bash-5.0.18 bug.sh
declare -A BUG1=([b]="1" [a]="1" )
declare -A BUG2=([b]="1" [a]="1" )
# bash-5.1.0 bug.sh
declare -A BUG1=([b]="" [a]="1" )
declare -A BUG2=([b]="" [a]="1" )
# 
Trilby wrote:

Do you have a different test-case that highlights your concern?

You are right, two cases gives same result that is differ from previous bash releases.

eschwartz wrote:

It's relevant in the sense that inside a function, it's unlikely to dirty future results in the same shell.

This looks like both inside and outside function, the result is the same. But something has changed between releases.

Last edited by solskog (2020-12-11 02:29:56)

Offline

#10 2020-12-11 02:39:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [Solved] bash 5.1.0(1)-release associative array


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2020-12-11 02:56:35

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [Solved] bash 5.1.0(1)-release associative array

oo. Fixed several issues with assigning an associative array variable using a
    compound assignment that expands the value of the same variable.

Thanks for pointing out the fix. How this could be a old bug instead of a old feature that is been removed from now on?

Offline

#12 2020-12-11 03:20:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [Solved] bash 5.1.0(1)-release associative array

It was not likely either.  It was likely an unintended side-effect of an inefficient approach to associative arrays.  As the implementation improves, side effects will change.  My money is on the change you observed being a result of the changes discussed in this thread:
https://lists.gnu.org/archive/html/bug- … 00121.html

But I really don't follow bash development.

Last edited by Trilby (2020-12-11 03:21:26)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#13 2020-12-11 04:02:42

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [Solved] bash 5.1.0(1)-release associative array

I see, this is a side effect for efficiency reason. Then, the changes maybe more beneficial then old behavior for some. I'll just need rewrite my old scripts. Thanks for both of you.

Last edited by solskog (2020-12-11 06:37:56)

Offline

#14 2020-12-12 09:37:26

GSMiller
Member
Registered: 2020-11-23
Posts: 75

Re: [Solved] bash 5.1.0(1)-release associative array

Solskog, some bug was fixed.
Please see Yesterday 02:39:34.


A dog is a man's best friend.

Offline

Board footer

Powered by FluxBB