Introduction
There are many, many, ways to create (pseudo)-random strings in Bash, but I found this approach utilizing shuf
to be the most understandable. Also, it gives excellent control over the output (avoiding whitespace and special characters) without resorting to the use of the tr
command to remove unwanted characters. And it seems to be available for many systems without requiring installation of additional packages.
The shuf command
When first learning about a command like shuf it is helpful to start by reading the tldr
and info
entries for the command. They provide excellent summaries of its basic functions.
According to:
tldr shuf
It “generate[s] random permutations."
And according to:
info shuf
"‘shuf’ shuffles its input by outputting a random permutation of its input lines. Each output permutation is equally likely."
Example 1: Generating a random number
To get started, let’s generate a random number in the range of 0-5.
There are short and long versions of the command that both do the exact same thing.
Long version:
shuf --echo --head-count=1 {0..5}
Short version:
shuf -e -n 1 {0..5}
Example 2: Generating a random letter
Likewise, we can generate a random letter with the following command.
Long version:
shuf --echo --head-count=1 {a..z}
Short version:
shuf -e -n 1 {a..z}
Example 3: Generating several random letters
Similarly, we can generate 5 random letters with the following command.
Long version:
shuf --echo --head-count=5 {a..z}
Short version:
shuf -e -n 5 {a..z}
It’s important to note that in this form the letters won’t repeat.
Example 4: Generating several random letters that might repeat
If, however, we want to generate 5 random letters with the possibility of the letters repeating, we can use the following command.
Long version:
shuf --echo --repeat --head-count=5 {a..z}
Short version:
shuf -er -n 5 {a..z}
Example 5: Generating a random string of letters
If we want to generate a random string of letters that might repeat (instead of separate letters), we can use the following command.
Long version:
shuf --echo --repeat --zero-terminated --head-count=5 {a..z}
Short version:
shuf -erz -n 5 {a..z}
This can be a little hard to read since it outputs the result on the same line as the next shell prompt.
Example 6: Generating a random string of letters with legible output
If we want to generate a random string of letters that might repeat (instead of separate letters) and output the result on it’s own line, we can append the echo
command.
Long version:
shuf --echo --repeat --zero-terminated --head-count=5 {a..z}; echo
Short version:
shuf -erz -n 5 {a..z}; echo
Thanks to this StackOverflow answer for help solving this.
Example 7: Generating a random alphanumeric string
Additional ranges can be added to make a random alphanumeric string.
Long version:
shuf --echo --repeat --zero-terminated --head-count=5 {A..Z} {a..z} {0..9}; echo
Short version:
shuf -erz -n 5 {A..Z} {a..z} {0..9}; echo
Example 8: Generating a random alphanumeric string of random length
We can nest the shuf command within itself to generate a random alphanumeric string of random length.
Long version:
shuf --echo --repeat --zero-terminated --head-count=$(shuf --echo --head-count=1 {0..5}) {A..Z} {a..z} {0..9}; echo
Short version:
shuf -erz -n $(shuf -e -n 1 {0..5}) {A..Z} {a..z} {0..9}; echo
Conclusion
The shuf command has many other uses. This is just a brief introduction to some of its basic functions.