Sample Header Ad - 728x90

Does `declare -a A` create an empty array `A` in Bash?

9 votes
1 answer
4671 views
Does declare -a A create an empty array A in bash, or does it just set an attribute in case A is assigned to later? Consider this code: set -u declare -a A echo ${#A[*]} echo ${A[*]} A=() echo ${#A[*]} echo ${A[*]} A=(1 2) echo ${#A[*]} echo ${A[*]} What should be the expected output? In Bash 4.3.48(1) I get bash: A: unbound variable when querying the number of elements after declare. I also get that error when accessing all the elements. I know that later versions of Bash treat this differently. Still I'd like to know whether declare actually *defines* a variable (to be empty).
Asked by U. Windl (1715 rep)
May 28, 2019, 10:42 AM
Last activity: May 28, 2019, 12:01 PM