Use recursion.
#! /bin/bash
array=(A B C)
words=(word1 word2)
combine () {
local depth=$1
local prefix=${2# }
shift 2
if ((depth == ${#array[@]})) ; then
echo "$prefix"
return
fi
local i
for (( i=1; i<=$#; ++i )) ; do
combine $((depth+1)) "$prefix${!i}${words[depth]}" "${array[@]}"
done
}
combine 0 "" "${array[@]}"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…