For loop and array in bash

Trying to output part of bash array

ARRAY=(1,2,3,4,5)
for((i=0;i<3;i++))do
    echo "${ARRAY[$i]}"
done

but in first loop echo output full array, and in the following loops output empty lines. How i can output array by elements in !this for cycle

Upvotes: 0

Views: 86

Answers (1)

karakfa
karakfa

Reputation: 67497

$ array=(1 2 3 4 5); for((i=0;i<3;i++)) do echo "${array[i]}"; done

1
2
3

space is the delimiter. Also, be careful using all uppercase identifiers, you may end up overwriting bash variables.

Upvotes: 3

Related Questions