FYI, since I'm reporting behavior of 4.2 and this appears fixed in 4.4. In 4.2, it seems that providing the =() as part of the declaration throws away the -g-ness of the declaration-within-a-function. I need to touch it after the declare:
(10:29)condor:~[518,18]$ bash -c 'function foo() { declare -g -A bar; bar[baz]=quux; }; foo; echo ${#bar[@]}' 1 (10:29)condor:~[519,19]$ bash -c 'function foo() { declare -g -A bar=(); bar[baz]=quux; }; foo; echo ${#bar[@]}' 0 (10:29)condor:~[520,20]$ bash -c 'function foo() { declare -g -A bar; bar=(); bar[baz]=quux; }; foo; echo ${#bar[@]}' 1 -----Original Message----- From: David Linden Sent: Thursday, November 7, 2024 12:42 AM To: Greg Wooledge <g...@wooledge.org> Cc: bug-bash@gnu.org Subject: RE: [EXTERNAL] - Re: This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo ${#foo[@]} Thanks, and for the quick reply. Language lawyers... I'm curious what motivated the change. Naively it seems creating a placeholder for a potential future array keeping track of attributes is more work than just creating an empty array. The change is "surprising" to a user, at least this user. -----Original Message----- From: Greg Wooledge <g...@wooledge.org> Sent: Wednesday, November 6, 2024 10:26 PM To: David Linden <dlin...@opentext.com> Cc: bug-bash@gnu.org Subject: [EXTERNAL] - Re: This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo ${#foo[@]} CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe. If you feel that the email is suspicious, please report it using PhishAlarm. On Thu, Nov 07, 2024 at 02:38:05 +0000, David Linden wrote: > Description: > This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo > ${#foo[@]} > How am I supposed to determine that a declared associative array is > empty? The easiest way would be to ensure that the array variable is actually created: set -u; declare -A foo=(); echo "${#foo[@]}" Without the =() part, declare -A only creates a placeholder, which will assign the -A flag to a future variable named foo, if one is ever created. You need an assignment to create the variable.