Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have

i=0
for true in DMGLIST[@]
	do
	let DMGSIZES2="${DMGSIZES}"*2
	let i++
done
unset i

echo ${DMGSIZES2[@]}

It does the calculation correctly for the first element, but after that it doesn't return anything at all. DMGSIZES2 seems to have only one element.

I have also tried it without quotes, but I get the same problem.

Any insight is appreciated. Also, any suggestions for better ways to do this are welcome. Thanks!

What contains DMGLIST ?
true as a variable name is odd.

I don't think I understand how thr variable fits in here. I have read a bunch of tutorials, but I just don't understand. For me it seems to get in the way, although obviously I did it wrong here. Can someone explain?

Please post a complete script. Without knowing what is in your arrays, it's difficult to figure out the issue.

Are you just trying to double number in an array and copying it to another array?

DMGLIST=(1 2 3 4)
for (( i = 0 ; i < "${#DMGLIST[@]}"; i++ )) 
do
    DMGLIST2[$i]=$((${DMGLIST[$i]}*2))
    echo "${DMGLIST2[$i]}";
done
[root@~]# bash array.sh 
2 4 6
[root@~]# cat array.sh 
#!/bin/bash
b=(1 2 3)
a=0
for i in ${b[@]}
do
    narray[${a}]=$((${b[$a]}*2))
    ((a++))
done
echo ${narray[@]}
 
Test environment : centos 5.5 64bit
GNU bash, version 3.2.25

Or

#!/bin/bash
b=(2 4 6)
a=0
for i in ${b[@]}
do
    narray[a++]=$((i*2))
done
echo ${narray[@]}
2 Likes

Perfect. I'm sure the other scripts accomplished the same thing, but this works and I understand it, and it demonstrates how to use the variable properly. Thanks so much!