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?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.