Random Word Generator

Sometimes a Linux user can be in need of a random word generator. Random words can be used to set a new password or simply to create a bunch of randomly named directories. If you need a single word, the easiest way is to visit an online Random Word Generator website. However, if you need to generate more words or automate your task, the Linux Bash shell can be a handy friend.

In this tutorial, you will see how to make a Bash script that can generate any number of random words on a Linux system. We will give you the code that you need for the script, and show you how to run it.

In this tutorial you will learn:

  • How to create a random word generator with Bash on Linux
  • How to execute the random word generator script
Random Word Generator
Random Word Generator
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software Random Word Generator Bash script
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Random Word Generator Bash Script

The following Vash random word generator uses /usr/share/dict/words as word input (feel free to use your own). This file should be available on all modern Linux operating systems.

Here is how this random word generator works:

1. The random word generator script first gets the total number of words available in the /usr/share/dict/words file.

2. Next, it will generate a random number in range from 0 to total number words available.

3. As a last step, the previously randomly generated number will be used by the sed command to print a line corresponding with a random number generated previously.

Note: If you have your own source of random words, you can enter it by altering the constant variable ALL_NON_RANDOM_WORDS.

Create a new script and paste the following code into it:


# Random Word Generator 
# linuxconfig.org 
if [ $# -ne 1 ] 
echo "Please specify how many random words would you like to generate !" 1>&2 
echo "example: ./random-word-generator 3" 1>&2 
echo "This will generate 3 random words" 1>&2 
exit 0
# Constants 
# total number of non-random words available 
non_random_words=`cat $ALL_NON_RANDOM_WORDS | wc -l` 
# while loop to generate random words  
# number of random generated words depends on supplied argument 
while [ "$X" -lt "$1" ] 
random_number=`od -N3 -An -i /dev/urandom | 
awk -v f=0 -v r="$non_random_words" '{printf "%i\n", f + r * $1 / 16777216}'` 
sed `echo $random_number`"q;d" $ALL_NON_RANDOM_WORDS 
  let "X = X + 1" 

We have saved our script with the name random-word-generator.sh, but of course you can use any name that you want.

After saving the file, you will need to give it executable permissions:

$ chmod +x random-word-generator.sh

Now we can execute the random word generator script. As an example, let’s generate 25 random words:

$ ./random-word-generator 25
Using our Random Word Generator Bash script on Linux
Using our Random Word Generator Bash script on Linux

Closing Thoughts

In this tutorial, we saw how to create a Bash sript to generate random words on Linux. This can be used to generate any number of random words, and the script can easily be edited in order to work with other source files that contain a list of words. Hopefully this will help you with whatever kind of project you have that requires you to generate random word data.

Comments and Discussions
Linux Forum