Goal
Generate a random string picked from a pattern
Recipe
Build the pattern, the random letter will be taken from it.
export ALPHABET="azertyuiopqsdfghjklmwxcvbn";
The length of the string
mihamina@pbmiha:~$ echo ${#ALPHABET} 26
Generate a random RANK to pick
RANK=$(($RANDOM % ${#ALPHABET}))
Pick one random letter fro the pattern
RANK=$(($RANDOM % ${#ALPHABET})); echo ${ALPHABET:$RANK:1}
Build a 20 characters long random string from the pattern
RANDSTRING=""; for I in $(seq 1 20) do RANK=$(($RANDOM % ${#ALPHABET})); RANDSTRING=${ALPHABET:$RANK:1}${RANDSTRING}; done; echo $RANDSTRING
Make a function to summarize it
function random_string () { ALPHABET="azertyuiopqsdfghjklmwxcvbn"; RANDSTRING=""; for I in $(seq 1 $1) do RANK=$(($RANDOM % ${#ALPHABET})); RANDSTRING=${ALPHABET:$RANK:1}${RANDSTRING}; done; } random_string 3 ; echo $RANDSTRING random_string 5 ; echo $RANDSTRING random_string 15; echo $RANDSTRING