bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

building arrays from non array variables using 'array[${#array[@]}]='


From: Mart Frauenlob
Subject: building arrays from non array variables using 'array[${#array[@]}]='
Date: Sun, 24 Jan 2010 11:13:13 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20100111 Thunderbird/3.0.1

Hello,

I'd like to ask, if the behavior of indexed array assignment using the
form: 'array[${#array[@]}]=' is as expected.
I tested bash versions 3.1.17 and 4.0.35.
Using v3.1 an explicitly declared non array variable (global or local)
results in an empty array[0] entry.
Using v4.0 the explicitly declared local non array variable results in
an array containing only the last value assigned to the array at index
array[1].

See test script and output below.

Thank you for time and interest.

Best regards

Mart


#-----------------------------------
# TEST SCRIPT - /tmp/arr_t

fill() {
for x in $str; do
    arr[${#arr[@]}]="$x"
done
for x in ${!arr[@]}; do
    printf "%s: %s\n" "$x" "${arr[x]}"
done
unset arr
echo
}

func() {
echo ${FUNCNAME}:
fill
}
func1() {
echo ${FUNCNAME}:
local -a arr
fill
}
func2() {
echo ${FUNCNAME}:
local arr
fill
}

str="a b c"

fill

declare -a arr
fill

declare arr
fill

func
func1
func2
#-----------------------------------

eris:~# bash --version
GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
eris:~# bash /tmp/arr_t
0: a
1: b
2: c

0: a
1: b
2: c

0:
1: a
2: b
3: c

func:
0: a
1: b
2: c

func1:
0: a
1: b
2: c

func2:
0:
1: a
2: b
3: c

eris:~# bash4 --version
GNU bash, version 4.0.35(1)-release (i686-pc-linux-gnu)
eris:~# bash4 /tmp/arr_t
0: a
1: b
2: c

0: a
1: b
2: c

0: a
1: b
2: c

func:
0: a
1: b
2: c

func1:
0: a
1: b
2: c

func2:
1: c




reply via email to

[Prev in Thread] Current Thread [Next in Thread]