Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active February 15, 2022 02:57
Show Gist options
  • Save ernstki/589bfb4adfb3869834c97bd106d58b7e to your computer and use it in GitHub Desktop.
Save ernstki/589bfb4adfb3869834c97bd106d58b7e to your computer and use it in GitHub Desktop.
Bash script to generate a mnemonic passphrase à la https://xkcd.com/936
#!/usr/bin/env bash
##
## generate mnemonic passphrases beginning with the given letters
##
## Author: Kevin Ernst
## Date: 27 May 2021
## License: MIT
## Source: https://gist.github.com/ernstki/589bfb4adfb3869834c97bd106d58b7e
##
## Usage: pphrases a b c [...]
##
# shellcheck disable=SC2016
_pphrases() {
local howmany=18
local shuf='shuf'
local sed='sed'
# use 'gshuf' on macOS
if [[ $(uname -s) == Darwin ]]; then
shuf='gshuf'
sed='gsed'
fi
for (( i=0; i<howmany; i++ )); do
# take each input letter,
for l in "$@"; do
# find one word that starts with that; omit hyphenated words and
# words that have periods in them
grep -i ^"$l" /usr/share/dict/words | grep -v '[-.]' | $shuf -n1
done | $sed -n 's/\(.\)\(.*\)/\U\1\L\2/;H;${x;s/^\n//;s/\n/-/g;p;}'
# ^ upper-case first character, then join with dashes
done | paste - - - | column -t
}
_pphrases "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment