Skip to content

Instantly share code, notes, and snippets.

@queertypes
Created October 3, 2018 17:43
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 queertypes/14e835a3801b609bde5cae22123a8626 to your computer and use it in GitHub Desktop.
Save queertypes/14e835a3801b609bde5cae22123a8626 to your computer and use it in GitHub Desktop.
A simple CLI password generator built on /dev/urandom and Unix tools
#!/bin/bash
# A simple password generator that uses a combination of /dev/urandom,
# tr, head, and xclip to quickly generate a reasonably random password
## strict bash mode
set -euo pipefail
IFS=$'\n\t'
## pwgen [count] [allowed-symbols]
### password length is 16 by default
readonly COUNT=${1:-16}
### use all symbols by default
readonly SYMBOLS=${2:-'/\-[]{};:",.<>?!@#$%^&*()=+\\'"'"}
### if you provide a non-empty value for the allowed symbols,
### guarantee that as least alphanumeric character will be in the
### password
readonly BASE_SYMBOLS='A-Za-z0-9'
readonly FULL_SYMBOLS="$BASE_SYMBOLS""$SYMBOLS"
## store generated password in clipboard
readonly PASS=$(< /dev/urandom tr -dc "$FULL_SYMBOLS" | head -c"$COUNT")
echo -n "$PASS" | xclip -sel clip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment