I need explenation about the following behavior of arrays in shell scripting:
Imagine the following is given:
arber@host ~> lsfileA fileB script.shNow i can do the following commands:
arber@host ~> ARR=($(ls -d file*))arber@host ~> echo ${ARR[0]} # start index 0arber@host ~> echo ${ARR[1]} # start index 1fileAarber@host ~> echo ${ARR[2]} # start index 2fileBBut when I do this via script.sh it behaves different (Start Index = 0):
arber@host ~> cat script.sh#!/bin/bashARR=($(ls -d file*))# get length of an arrayaLen=${#ARR[@]}# use for loop read all items (START INDEX 0)for (( i=0; i<${aLen}; i++ ));do echo ${ARR[$i]}doneHere the result:
arber@host ~> ./script.shfileAfileBI use Ubuntu 18.04 LTS and zsh. Can someone explain this?