c=('a' 'c') n=(3 4 ) for i in $(seq 0 $((${#c[*]}-1))) do echo ${c[i]} ${n[i]} done
Sometimes itβs more convenient.
To explain the ugly part, as noted in the comments:
seq 0 2 creates a sequence of numbers 0 1 2. $ (cmd) is command substitution, therefore, for this example, seq 0 2 is displayed, which is a numerical sequence. But what is the upper bound, $((${#c[*]}-1)) ?
$ ((somthing)) is an arithmetic extension, so $ ((3 + 4)) is 7, etc. Our expression ${#c[*]}-1 , so something is 1. It's pretty simple if we know what ${#c[*]} .
c is an array, c [*] is just an array, $ {# c [*]} is the size of the array, which in our case is 2. Now we discard everything: for i in $(seq 0 $((${#c[*]}-1))) for i in $(seq 0 $((2-1))) for i in $(seq 0 1) is for i in 0 1 . Since the last element in the array has an index, which is the length of the array - 1.
user unknown Mar 15 '12 at 11:22 2012-03-15 11:22
source share