-1

I'm very new to shell script. I'm learning the basic of it. My very simple for loop is not working. It always stops at the first iteration. I already follow the document to create an array variable and using for loop with super simple code.

#!/bin/bash

LIST=()
LIST+=('aaa') 
LIST+=('bbb') 
LIST+=('ccc') 

for i in $LIST
do
    echo '----------'$i'----------'
done

It only show 'aaa' then stop the loop. I really have no idea. Please help.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Hikaru Shindo
  • 2,611
  • 8
  • 34
  • 59

1 Answers1

4

$LIST expands to the first element in array LIST, it's basically the same thing as ${LIST[0]}. You need to use ${LIST[@]} in double-quotes to get each element as a separate word, like:

#!/bin/bash

LIST=()
LIST+=('aaa') 
LIST+=('bbb') 
LIST+=('ccc') 

for i in "${LIST[@]}"
do
    echo '----------'"$i"'----------'
done

c.f. Bash Reference Manual ยง Arrays

oguz ismail
  • 1
  • 16
  • 47
  • 69