random-words 643 B

12345678910111213141516171819202122
  1. #!/usr/bin/env bash
  2. # This function will create a random word pair with an underscore separator ex. turtle_ladder
  3. # It accepts one optional argument (an integer) (the number of words to return)
  4. random_words() {
  5. local num=${1:-2}
  6. local -a arr
  7. for ((i=0;i<num;i++)); do
  8. word=$(shuf -n1 /usr/share/dict/words | sed -e 's/-//g' -e 's/_//g')
  9. # Sanitize
  10. word="${word//-/}"
  11. word="${word//_/}"
  12. word="${word,,}"
  13. arr+=("$word")
  14. done
  15. printf "%s_" "${arr[@]}" | sed 's/_$//'
  16. }
  17. # Allow this file to be executed directly if not being sourced
  18. if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  19. random_words "$@"
  20. exit $?
  21. fi