Skip to content

Instantly share code, notes, and snippets.

@nickolasburr
Created December 1, 2017 20:46
Show Gist options
  • Save nickolasburr/a49073af159254f784c17673619b99c0 to your computer and use it in GitHub Desktop.
Save nickolasburr/a49073af159254f784c17673619b99c0 to your computer and use it in GitHub Desktop.
Generate pseudo-random string of n characters.
#!/usr/bin/env bash
###
### prsg: Generate pseudo-random string of n characters.
###
if [[ $# -eq 0 ]]; then
printf 'Usage: prsg LENGTH\n'
exit 1
fi
LENGTH="$1"
REGEX='^[0-9]+$'
if ! [[ $LENGTH =~ $REGEX ]]; then
printf 'Argument must be an integer\n\nUsage: prsg LENGTH\n'
exit 1
fi
LC_CTYPE=C tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c $LENGTH | xargs ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment