Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Last active Oct 11, 2015
Embed
What would you like to do?
standalone random string function
# Print or assign a random alphanumeric string of a given length.
# rndstr len [ var ]
function rndstr {
if [[ $FUNCNAME == "${FUNCNAME[1]}" ]]; then
unset -v a l
printf "$@"
elif [[ $1 != +([[:digit:]]) ]]; then
return 1
elif (( ! $1 )); then
return
else
typeset -a a=({a..z} {A..Z} {0..9}) l=${#a[@]}
eval '${2:+"$FUNCNAME" -v} "${2:-printf}" -- %s "${a[RANDOM%l]"{1..'"$1"'}"}"'
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment