Created
July 10, 2017 18:44
-
-
Save tannerwelsh/d541dcbab7bf1fced4d661ea18c71079 to your computer and use it in GitHub Desktop.
Get a random word of length n
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
#!/usr/bin/env sh | |
word_length=$1 | |
all_words=/usr/share/dict/words | |
# use outer wrapping parens to convert to an array | |
words=( $(grep -E "^.{$word_length}$" $all_words) ) | |
num_words=${#words[@]} # get length of words array | |
index=$(($RANDOM % $num_words)) # make a random index | |
echo ${words[$index]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment