Question: what does this line do? Can someone please explain?

 if [ $((${array[$i]}+1)) -ne ${array[$(($i + 1))]} ] ;then foo; fi
  • same as if ((array[i] + 1 != array[i+1])); then foo; fi (as long as $i and array[i+1] contain decimal integers). – Stéphane Chazelas Aug 4 '16 at 8:49
  • It produces the value at position [$i] in $array, adds one to it - inside an arithmetic context, then returns (via command substitution) that as the LHS operand to the -ne conditional operator, then evaluates the RHS operand, $(($i + 1)) is variable $i + 1 then the result of this arithmetic operation is used as the key/position in the array ${array[]} - if the LHS and the RHS results are -ne "not equal` the then block runs – the_velour_fog Aug 4 '16 at 8:57
  • And also the same as if [[ array[i]+1 -ne array[i+1] ]] ;then foo; fi as both sides of an integer operator are processed as arithmetic expansions. In math: test for A[i]+1 = A[i+1]. Or, in words: is the next array element the consecutive numeric value of the present one? – user79743 Aug 4 '16 at 11:46

[ ... -ne ... ] - test for inequality
$(( ... + 1)) - add one, arithmetic expansion
${array[$i]} - reference to element $i of an array variable

Or in other words the test is A[i] + 1 != A[i + 1]

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.