0

I have written some script in bash but it won't work. Can anybody help me with this. I am providing the script here:

#!/bin/bash
declare -a array=("red" "blue" "green" "yellow")

for (( i=0; i<${array[@]}; i++));
do
        echo "items: $i"
done

I want to iterate through the array. Coz when ever I do it I'm getting an error saying:: arr1.sh: 2: Syntax error: "(" unexpected

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

2

Try this:

$ cat iterate_array.sh 
#!/bin/bash
declare -a array=("red" "blue" "green" "yellow")
for  i in ${!array[@]}; do
        echo ${array[$i]}
done
$ ./iterate_array.sh 
red
blue
green
yellow

Is that what you want?

Halley Oliveira
  • 219
  • 2
  • 3