Problem with simple array

Why doesn’t this work?

I’m just trying to get char to print the contents of the array, which is the alphabet.

alphabet=(a b c d e f g h i j k l m n o p q r s t u v w x y z)

for char in alphabet
do

  echo $char
  (($char++))

done

<-This just prints the word “alphabet”

"declare -p alphabet" 
"${!alphabet[@]}"
"${#alphabet[@]}"

← these statements work outside the loop, so it seems that the array can be browsed.

If I use for (( char=0; char < "${#alphabet[@]}"; char++ )) it lists the correct number of the array elements; 0 - 25.

What am I doing wrong?

Any help is appreciated!

  • (( is for arithmetic operators. You shouldn’t be using it for string concatenation.
  • Your second loop is a very long way to set char=${#alphabet[@]}.
  • for char in alphabet loops over the single string, “alphabet”. You probably want for char in "${alphabet[@]}" to loop over the array. Or you can use brace expansion and get rid of that array: for char in {a..z}