Bash比较数组中的所有值

时间:2017-11-03 17:38:16

标签: arrays bash

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

列出所有可能的组合,没有重复,数组不按顺序排列,输出不需要按顺序排列。

  

期望的输出

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

1 个答案:

答案 0 :(得分:2)

使用 bash for循环播放:

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 

输出:

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