Skip to content

Instantly share code, notes, and snippets.

@tannerwelsh
Created July 10, 2017 18:44
Show Gist options
  • Save tannerwelsh/d541dcbab7bf1fced4d661ea18c71079 to your computer and use it in GitHub Desktop.
Save tannerwelsh/d541dcbab7bf1fced4d661ea18c71079 to your computer and use it in GitHub Desktop.
Get a random word of length n
#!/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