I want to generate N letter permutations of a given string using bash scripting
I have successfully written a code to find the permutation of a given word. However, I can't seem to get the permutation up to N letters. Note: I have to avoid the use of sed and awk commands. Here's what I have tried:
#!/bin/bash
x=$1
counter=0
ARRAY=()
function permutate {
if [ "${#1}" = 1 ]; then
echo "${2}${1}"
ARRAY+=("${2}${1}")
else
for i in $(seq 0 $((${#1}-1)) ); do
pre="${2}${1:$i:1}"
seg1="${1:0:$i}"
seg2="${1:$((i+1))}"
seg="${seg1}${seg2}"
permutate "$seg" "$pre"
done
fi
}
permutate $x
For example, if I have a word "JACK" and I want 3 letter permutations then it should give: JAC KJA JAK etc... But I can't seem to get to the bottom of it.