[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo ${#foo[
From: |
David Linden |
Subject: |
This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo ${#foo[@]} |
Date: |
Thu, 7 Nov 2024 02:38:05 +0000 |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-redhat-linux-gnu'
-DCONF_VENDOR='redhat' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL
-DHAVE_CONFIG_H -I. -I. -I./include -I./lib -D_GNU_SOURCE -DRECYCLES_PIDS
-DDEFAULT_PATH_VALUE='/usr/local/bin:/usr/bin' -DSYSLOG_HISTORY -O2 -g -pipe
-Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS
-fexceptions -fstack-protector-strong -grecord-gcc-switches
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1
-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic
-fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection
-Wno-parentheses -Wno-format-security
uname output: Linux engdev2 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Wed Sep 11
18:02:00 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu
Bash Version: 4.4
Patch Level: 20
Release Status: release
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?
Or even use it in a conditional even one where the value won't be
evaluated?
Repeat-By:
Bash 4.2:
(21:10)$ bash --version; bash -c 'set -u; declare -A foo; echo
${#foo[@]}; echo $((0 && ${#foo[@]}))'
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute
it.
There is NO WARRANTY, to the extent permitted by law.
>>> 0
>>> 0
Bash 4.4:
(21:14)$ bash --version; bash -c 'set -u; declare -A foo; echo
${#foo[@]}'
GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute
it.
There is NO WARRANTY, to the extent permitted by law.
>>> bash: foo: unbound variable
(21:14)$ bash --version; bash -c 'set -u; declare -A foo; echo
$((0 && ${#foo[@]}))'
GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute
it.
There is NO WARRANTY, to the extent permitted by law.
>>> bash: foo: unbound variable
Perhaps related, ${#foo[@]:-0} is now considered a bad substitution; in 4.2 it
was OK.