Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Last active June 25, 2023 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mttjohnson/43b7ccf44601889a74d4063048e9cb68 to your computer and use it in GitHub Desktop.
Save mttjohnson/43b7ccf44601889a74d4063048e9cb68 to your computer and use it in GitHub Desktop.
Generate a password from the shell
# Defined function for generating random password
genpasswd() {
local l=$1
[ "$l" == "" ] && l=20
LC_CTYPE=C tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}
NEW_PASSWORD=$(genpasswd 16)
echo $NEW_PASSWORD
# Alternate condensed form
NEW_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9_' < /dev/urandom | head -c 16)
NEW_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9%&()*+,-./:;<>?@[]^_{}~' </dev/urandom | head -c 16)
echo $NEW_PASSWORD
# Generate 5 random words
WORDS=5; LC_ALL=C grep -x '[a-z]*' /usr/share/dict/words | shuf --random-source=/dev/urandom -n ${WORDS} | tr '\n' ' ' | sed 's/ $/\n/'
# Alternate using uuidgen
NEW_PASSWORD=$(uuidgen)
echo "NEW_PASSWORD: ${NEW_PASSWORD}"
# Alternate using pwgen
NEW_PASSWORD=$(pwgen -A 16 1)
echo "NEW_PASSWORD: ${NEW_PASSWORD}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment