I´m trying to dynamically add an element into an array:
array=("element1" "element2" "element3")
fa=()
# now loop through the above array
for i in "${array[@]}"
do
fa+=("$i")
# or do whatever with individual element of the array
done
echo $fa
But it's returning element1
.
I've tried with an index, but I'm getting the same result:
fa[index]="$i"
((index++))
Am I doing something wrong here?
The problem is with printing ie echo $fa
. This is equivalent to echo ${fa[0]}
which means the first element of the array, so you gotelement1
echo "${fa[@]}"
should give you the entire array.
Reference
[ This ] should give you a nice description about bash arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With