Skip to content

Instantly share code, notes, and snippets.

@kojiromike
Created September 22, 2014 14:08
Show Gist options
  • Save kojiromike/bd17a882d06b104f6a3d to your computer and use it in GitHub Desktop.
Save kojiromike/bd17a882d06b104f6a3d to your computer and use it in GitHub Desktop.
Generate random passwords on a POSIX system
#!/bin/bash
##
# Generate a random password on a UNIX system.
#
# See the regex or re_format documentation for the syntax of
# "chars".
#
# Example:
#
# Generate a 12 character password with only capital ASCII letters:
# ./randpass 12 A-Z
#
# Generate an 8 character password with any printable ASCII character:
# ./randpass 8 '[:print:]'
#
# Generate a 16 character alphanumeric password:
# ./randpass 16 '[:alnum:]'
main() {
local -x LC_CTYPE=C
local len="$1"
local chars="$2"
tr -dc "$chars" < /dev/urandom | dd bs="$len" count=1 2>/dev/null
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment