Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Last active October 11, 2015 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/3929316 to your computer and use it in GitHub Desktop.
Save ormaaj/3929316 to your computer and use it in GitHub Desktop.
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