Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 7072

Behavior of Arrays in bash scripting and zsh shell (Start Index 0 or 1?)

$
0
0

I need explenation about the following behavior of arrays in shell scripting:

Imagine the following is given:

arber@host ~> lsfileA fileB script.sh

Now 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 2fileB

But 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]}done

Here the result:

arber@host ~> ./script.shfileAfileB

I use Ubuntu 18.04 LTS and zsh. Can someone explain this?


Viewing all articles
Browse latest Browse all 7072

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>