Created
September 22, 2014 14:08
-
-
Save kojiromike/bd17a882d06b104f6a3d to your computer and use it in GitHub Desktop.
Generate random passwords on a POSIX system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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