Table of Contents

About

The following sections describe ways, depending on what tools are available, to generate random primitives on the command line such as numbers or strings.

Primitive

We define primitives as primitive types that might be one-level up in terms of specialization; for instance, an UUID is just a hexadecimal string, but in this definition it is considered a primitive type. The same applies to MD5.

Numbers

Command Description
$((RANDOM)) a bashism: a random number in the interval $[0, 65535]$ or $[0, 32760]$
$(( RANDOM % (10 - 5 + 1 ) + 5 )) a random number in the interval $[5, 10]$ using clamping
shuf –random-source='/dev/random' -i 1-100 -n 1 generates one (-n 1) random number in the interval $[1, 100]$ (-i 1-100)
jot -r 1 1 100 generate one (-r 1) random number in the interval $[1, 100]$
awk -v min=5 -v max=10 'BEGIN{srand(); print int(min + rand() * (max - min + 1))}' generates a random number between $5$ and $10$ (-v min=5 -v max=10) using a random number generator seeded by time (srand()) and then clamping the random number of a range
tr -cd '[:digit:]' < /dev/random | fold -w 11 | head -n 1 one (head -n 1) 11-digit long (fold -w 11) number

Notes

UUID

Command Description
cat /proc/sys/kernel/random/uuid a random UUIDv4 from the kernel
uuidgen found in package uuid-runtime on Debian and derivatives
uuid another UUID generating package

Random MD5 Hash

Command Description
dd if=/dev/random status=none count=1 bs=512 | md5sum generates a random MD5 hash with $512$ bytes (bs=512) of entropy from /dev/random

Strings

Command Description
tr -dc 'A-Za-z0-9' < /dev/random | fold -w 5 | head -n 1 generate one ( head -n 1) random string of length 5 (fold -w 5) using the character class A-Za-z0-9 (-dc A-Za-z0-9)
tr -dc '[:graph:]' < /dev/random | head -c 10 generate $10$ characters (-c 10) of printable characters except space ([:graph:])
shuf -er -n20 {A..Z} {a..z} {0..9} | paste -d'\0' -s - using Bourne shell character sequences; generates a 20 character long (-n20) mixed-case alpha-numeric ({A..Z} {a..z} {0..9}) password
openssl rand 99999 | tr -dc '[:alnum:]' | head -c 5 generates a 5-character long alpha-numeric ([:alnum:]) password (head -c 5)((openssl is just used to gather entropy and much of that entropy (99999 bytes) will not fall within the alphabet selected by tr such that it is not clear how many bytes to select)
openssl rand -base64 12 or openssl rand -hex 12 openssl can generate random strings but they will either be base64 or hexadecimal