For looping over an array in bash

If I want to print the content from item 13 to the second last item in the array, and I don't know how long the array will be, how will this be done with BASH?

for array_element in `seq 13 $(({myarray[@]-1))`
do
   echo ${myarray[array_element]}
done

      

+3


source to share


2 answers


Since you are using bash

, don't use seq

. Use a C loop instead.



for ((i=13; i < ${#myarray[@]} - 1; i++)); do
    echo ${myarray[i]}
done

      

+2


source


You can do it like this:



    for array_element in `seq $((${#myarray[@]}-1))`
    do
       echo ${myarray[$((array_element-1))]}
    done

      

0


source







All Articles