0

I have this bash code:

declare -a globalDir
#------------------------------#
#Code that fills globalDir array
#-----------------------------#

echo ${#globalDir[@]} #print globalDir lenght and I get 2


for element in $globalDir
do

    echo "$element"

done

My problem is that the code does not show any element of globalDir even if it's lenght is 2

mario razzu
  • 333
  • 1
  • 5
  • 12
  • Should be `for element in "${globalDir[@]}"`. Even then it should print at least the first element as it is. You sure you don't see any output and your array is populated? – P.P Jun 11 '15 at 13:26
  • Ok with for element in "${globalDir[@]}" it works. I have a dubt : for populate array I use this code for folder in $allDirectory do if [[ "$folder" =~ ^$local ]]; then ((count++)) globalDir[$count]=$folder fi done Why in this case I use the for "for folder in $allDirectory" and not "for element in "${globalDir[@]}". – mario razzu Jun 11 '15 at 13:30
  • Right. You want increment `count` *after adding the value to the array. Remember indexes start from `0` in bash. – P.P Jun 11 '15 at 13:32
  • I edited previous comment – mario razzu Jun 11 '15 at 13:33

0 Answers0