function permute { if (( $# == 1 )); then set -- $(seq $1) fi local A=("$@") permuteAn "$#" } function permuteAn { # print all permutations of first n elements of the array A, with remaining # elements unchanged. local -i n=$1 i shift if (( n == 1 )); then printf '%s\n' "${A[*]}" else permuteAn $(( n-1 )) for (( i=0; i