0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Generate Random String Easily

Use head -c 12 /dev/urandom | tr -dc A-Za-z0-9 | head -c 12 in Bash to generate a random 12-character string.
📋

Examples

Inputlength=8
OutputaB3dE9fG
Inputlength=12
OutputX7kLm9PqRs2T
Inputlength=4
OutputZ1xQ
🧠

How to Think About It

To generate a random string in Bash, you read random bytes from a system source like /dev/urandom, then filter out unwanted characters using tr, and finally select the desired number of characters with head -c.
📐

Algorithm

1
Read random bytes from the system's random source.
2
Remove all characters except letters and digits.
3
Cut the filtered output to the desired string length.
4
Print the resulting random string.
💻

Code

bash
#!/bin/bash
length=12
random_string=$(head -c 100 /dev/urandom | tr -dc A-Za-z0-9 | head -c "$length")
echo "$random_string"
Output
X7kLm9PqRs2T
🔍

Dry Run

Let's trace generating a random string of length 4 through the code

1

Read random bytes

head -c 100 /dev/urandom outputs random bytes like '\x4a\x7f\x21...'

2

Filter characters

tr -dc A-Za-z0-9 removes non-alphanumeric characters, leaving 'Z1xQ...' sequence

3

Cut to length

head -c 4 takes first 4 characters: 'Z1xQ'

StepOperationResult
1Read random bytes\x4a\x7f\x21\x5a\x31\x78\x51...
2Filter to alphanumericZ1xQ...
3Cut to length 4Z1xQ
💡

Why This Works

Step 1: Read random bytes

The command head -c 100 /dev/urandom reads raw random data from the system.

Step 2: Filter characters

Using tr -dc A-Za-z0-9 removes all characters except letters and digits, making the string readable.

Step 3: Limit length

The head -c command cuts the output to the desired number of characters.

🔄

Alternative Approaches

Using openssl rand
bash
openssl rand -base64 12 | tr -dc A-Za-z0-9 | head -c 12
This uses OpenSSL to generate random bytes and then filters them; it may be faster but requires OpenSSL installed.
Using /dev/urandom with fold
bash
cat /dev/urandom | tr -dc A-Za-z0-9 | fold -w 12 | head -n 1
This method folds the filtered characters into lines of length 12 and picks the first line.
Using Bash built-in $RANDOM
bash
for i in {1..12}; do printf '%x' $((RANDOM%16)); done
This generates a hex string using Bash's built-in random number generator but is less random and only hex digits.

Complexity: O(n) time, O(n) space

Time Complexity

The script reads and processes a fixed number of bytes proportional to the desired string length, so time grows linearly with string length.

Space Complexity

Memory usage grows linearly with the output string length since it stores the filtered characters.

Which Approach is Fastest?

Using head /dev/urandom with tr is fast and simple; openssl rand may be faster but requires extra software.

ApproachTimeSpaceBest For
head /dev/urandom + trO(n)O(n)Simple, no extra tools
openssl randO(n)O(n)Faster, requires OpenSSL
Bash $RANDOM loopO(n)O(n)Hex strings, no external commands
💡
Always filter random bytes to readable characters to avoid control characters in your string.
⚠️
Beginners often forget to filter out non-alphanumeric characters, resulting in unreadable or problematic strings.