nyitguy
nyitguy

Reputation: 608

Bash compare all values in array

#!bash
ARRAY=(one two three four five)

List all possible combinations with no repetition and array is not in order and the output does not need to be in order.

Desired output

one two
one three
one four
one five
two three
two four
two five
three four
three five
four five

Upvotes: 1

Views: 441

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

With bash for looping:

arr=(two four one three five)
len=${#arr[*]}

for (( i=0; i < $len; i++ )); do
    for (( j=$((i+1)); j < $len; j++ )); do 
        echo "${arr[$i]} ${arr[*]:$j:1}"
    done
done 

The output:

two four
two one
two three
two five
four one
four three
four five
one three
one five
three five

Upvotes: 2

Related Questions