3

I'm trying to add a text to every value in array while I loop thru them.

I tried this:

for value in "${array[@]}"
do
    echo "--" "$value"
done

It will only add "--" one time. And that's in the beginning of the output.

I've tried printf also, like this:

printf "%s--" "${array[@]}"

Same result.

I add my values like this:

array+="1"
array+="2"
array+="3"

I declare my array like this:

array=()

Also tried:

declare -a array

Is this the correct behaviour, or am I doing something wrong?

0

1 Answer 1

6

To add to an array, you need to use parentheses:

array+=(1)
array+=(2)

The way you used you added all the values to the first element of the array.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .