Table of Contents
Using $RANDOM Variable
Use the $RANDOM variable with cut command to generate random string in Bash.
|
1 2 3 4 |
random_string=$(printf '%s' $(echo "$RANDOM" | md5sum) | cut -c 1-10) echo $random_string |
|
1 2 3 |
d41d8cd98f |
Bash provides a built-in variable $RANDOM that generates random numbers between 0 and 32767. We can use this variable to generate random strings of any length. Here’s how to generate a random string of length 10. The above code’s working is as follows:
echo "$RANDOM" | md5sumgenerates a 32-character MD5 hash from the$RANDOMvariable.printf '%s'converts the hash to a string.cut -c 1-10selects the hash’s first10characters. And theechocommand displays output on the console.
Use /dev/urandom Command
Use the /dev/urandom command to generate a random string in Bash.
|
1 2 3 4 |
random_string=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1) echo $random_string |
|
1 2 3 |
TOBNlpuQY4 |
The above code’s working is as follows:
- The
tr -dc 'a-zA-Z0-9'command filters the output of/dev/urandomto include only alphanumeric characters, i.e., letters (both uppercase and lowercase) and digits. - The
fold -w 10command wraps the output oftrafter every10characters, creating a new line for each 10-character segment. - Finally, the
head -n 1command selects the first line of the output, which contains the randomly generated string of10characters.
Using openssl Command
Use the openssl command to generate a random string in Bash.
|
1 2 3 |
openssl rand -base64 15 |
|
1 2 3 |
7c07QKJgcb7R6fj |
The above code’s working is as follows:
- The
openssl randcommand generates random data. - The
-base64encodes the generated data in Base64 format. - And
15specifies the number of bytes to generate, which translates10characters inBase64format.
Further reading:
Using uuid Command
Use uuid to generate a random string in Bash.
|
1 2 3 |
cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 20; echo; |
|
1 2 3 |
ecc7c8ba0d7d4134b52e |
The above code’s working is as follows:
- The
cat /proc/sys/kernel/random/uuidcommand generates a random UUID (Universally Unique Identifier) using the kernel’s random number generator and outputs it to the console. - This part
| sed 's/[-]//g'of the command pipes the previous command’s output to thesedcommand, which searches for any hyphens in the output and replaces them with nothing (i.e., removes them). Thegflag means to do this globally (i.e., for all occurrences of hyphens in the output). - This part
| head -c 20of the command pipes the output of thesedcommand to theheadcommand, which takes the output’s first20characters and prints them to the console. - And
; echoprints a newline character to the console after the random string
Conclusion
Considering the above solutions, we have explored four different methods to generate random strings in Bash. These methods include using the $Random, /dev/urandom, openssl, and uuid commands. You can choose the method that best suits your needs. Using these methods, you can generate random strings for various applications, such as password generation and token authentication.