3

If I want to print out the contents from the 13 element till the second last element of an array and I don't know how long the array is, is this how it would be done with BASH?

for array_element in `seq 13 $(({myarray[@]-1))`
do
   echo ${myarray[array_element]}
done
2
  • No closing brace on this $(({myarray[@]-1)) Commented Apr 27, 2015 at 9:32
  • 1
    This is one valid way of it. Just change $(({myarray[@]-1)) to $((${#myarray[@]}-1)) Commented Apr 27, 2015 at 9:32

2 Answers 2

2

Since you're using bash, don't use seq. Instead, use a C-style for loop.

for ((i=13; i < ${#myarray[@]} - 1; i++)); do
    echo ${myarray[i]}
done
Sign up to request clarification or add additional context in comments.

Comments

0

You could do it like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.