1. Overview

In this tutorial, we’ll learn how to generate random strings that consist of combinations of characters.

First, we’ll look at a Bash script to generate a random string using $RANDOM. Next, we’ll discuss various methods to generate random strings without $RANDOM.

2. Use $RANDOM to Generate a Random String

This is the first method to generate a random sequence of strings using $RANDOM$RANDOM is a built-in shell variable that is capable of giving a pseudorandom number. We can use this variable to generate a sequence of random numbers:

$ echo $RANDOM

In this method, we’ll create a Bash script. This script will use an array sparse method to generate a random sequence using $RANDOM. After that, we’ll print the random sequence:

for i in {a..z} {A..Z} {0..9}; 
printf %s ${array[@]::8} $'\n'

Lastly, we’ll execute the script:

$ ./random

This script will generate one sequence of random characters. To generate several sequences, we’ll perform the same in the nested loop. After that, we’ll pipe the output fold and head command:

while ((${#b[@]} <= 32768)); 
   for i in {a..z} {A..Z} {0..9}; 
tr -d ' ' <<< ${b[@]} | fold -w 8 | head -n 4096

Alternatively, we can calculate the minimum and maximum length using the $RANDOM. We first defined a list of uppercase letters, lowercase letters, digits, and special characters. Next, we calculated the minimum and maximum character limit randomly using the $RANDOM variable. Lastly, we’ve printed the output string:

set -A CHARS a b c d e f g h i j k l m n o p q r s t u v w x y z \
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
0 1 2 3 4 5 6 7 8 9 \; \: \. \~ \! \@ \# \$ \% \^ \& \* - + = \?
let POSCNT=0;
while [ 1 -eq 1 ]
    if [ $POSCNT -ge $MAXLEN ]
    let POSCNT=$POSCNT+1
echo "password=${RANDSTR} length=${MAXLEN}"
return 0

Then we’ll execute this script:

$ ./random
password=wNxWzgHfLP length=10
password=e6LU3FFv length=8
password=MOH21WSYGEjVFKBOlmBA length=20
password=9oqgx3vl7l0km92rxs length=18
password=py62dg9c7zbvas8a length=16
password=bkeysn8q3d1n length=12
password=epbx6295xvew0l145nb6 length=20

This script is very useful in generating strong passwords for any use.

3. Use a Fixed Set of Characters

The second method to generate random characters is to create a Bash script and iterate through a possible number of characters. For this, we’ll first create a variable consisting of a set of characters. Next, we’ll use the parameter expansion along with the modulo operator. For instance, to generate the random characters with the characters abcd1234ABCD:

for i in {1..8} ; do
    echo -n "${chars:RANDOM%${#chars}:1}"

Next, we’ll run the Bash script after making it executable:

$ ./random

It is crucial to note that $RANDOM could be larger in length. With the help of a modulo operator, the result will not exceed the size of the variable chars.

4. Use MD5 Hash to Generate a Random String

Another method we can utilize to generate random strings is to use the md5 checksum. To use md5 hash, we’ll first take the $RANDOM and pipe it to the md5sum. It will generate a random string which we can view by piping to the head command. To illustrate:

$ echo $RANDOM | md5sum | head -c 20

The argument 20 in the head command will display the 20 characters from the md5 hash.

5. UUID Kernel Generator

The Kernel UUID generator also gives Linux users a unique hexadecimal value that we can convert to a random string. It is located at /proc/sys/kernel/random/uuid. We’ll use this with the sed and head command:

$ cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 20; echo;

We can see the first 20 characters of the generated string. It involves a random combination of characters.

6. Generate a Random String Using urandom

We can also use the devices as files to generate a random number. Since everything in Linux is treated as a file, we can utilize this feature. By default, the files located in /dev are known as pseudo devices. These devices act as an intermediary between the kernel and the hardware.

The urandom is a special file located at /dev. This file provides access to the kernel’s random number generated. We can also use this to create a random string.

To generate a random 32-character string, we’ll use the /dev/urandom file in combination with the head and fold command:

$ cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1

The tr command picks the particular characters from the standard input and pipes the output to the fold command. The fold command then folds the output according to the limit given. In addition, the head command displays only the first line of the output.

Argument 32 with the fold command shows the random string of length 32, and argument indicates the number of strings that appear on the screen. This random number includes both the uppercase and the lowercase characters. However, to generate a random string consisting of lowercase letters only, we will modify the tr command:

$ cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 20 | head -n 5

In addition, we can also generate random strings consisting of digits. For example, to generate the random string consisting of digits from 0-9, we’d modify the tr command:

$ cat /dev/urandom | tr -dc '0-9' | fold -w 20 | head -n 6 | head --bytes 1

This shows the random string consisting only of digits.

7. Conclusion

In this article, we discussed creating a series of random characters with and without using $RANDOM. First, we’ve covered the usage of the $RANDOM variable. After that, we discussed creating a random string using a Kernel UUID generator, md5 checksum, and pseudo devices.

We learned that depending on the use case, the users could opt for any of these methods. The shell script discussed in section 2 is particularly useful for Linux system administrators to generate strong passwords.

Comments are closed on this article!