$ openssl rand -hex 16 | sed ‘s/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*/\1\2\3\4-\5\6-\7\8-/’
# Note: openssl rand does not set version/variant bits — not RFC-strict
⚠️
openssl rand output is not a real UUID
The openssl rand approach produces random hex data formatted to look like a UUID, but it does not set the required version (4) and variant (8/9/a/b) bits. For a spec-compliant UUID, use uuidgen or /proc/sys/kernel/random/uuid instead.
Platform Differences
uuidgen on Linux vs macOS
Platform
Command
Notes
Linux (most distros)
uuidgen
Part of util-linux, usually pre-installed. Install via apt install uuid-runtime if missing.
macOS
uuidgen
Pre-installed on all macOS versions. Outputs uppercase by default.
Windows (PowerShell)
[guid]::NewGuid()
Not a Bash command, but the PowerShell equivalent for Windows users.
WSL / Git Bash
uuidgen
Available if util-linux is installed in the WSL distro.
Bash — install uuidgen on Debian/Ubuntu if missing
$ sudo apt install uuid-runtime
Scripting
Using uuidgen in Shell Scripts
Assign a generated UUID to a variable, or generate many at once for bulk file naming or test data:
for i in $(seq 1 10); do
uuidgen
done > uuids.txt
# Or a one-liner:
for i in {1..10}; do uuidgen; done
Bash — validate UUID format with regex
is_valid_uuid() {
[[ “$1” =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ ]]
}
if is_valid_uuid “550e8400-e29b-41d4-a716-446655440000”; then
echo “Valid”
fi
FAQ
Bash UUID FAQ
Run uuidgen in your terminal. It is part of the util-linux package and is pre-installed on most Linux distributions. If it is missing, install it with sudo apt install uuid-runtime on Debian/Ubuntu systems. It generates a random UUID v4 by default.
Yes. On Linux, run cat /proc/sys/kernel/random/uuid to read a fresh random UUID directly from the kernel, no package installation required. This works on any Linux system with a standard kernel, including minimal containers.
Mostly yes. uuidgen is pre-installed on macOS and produces the same RFC-compliant UUID v4 format. One difference: macOS uuidgen outputs uppercase letters by default, while Linux typically outputs lowercase. Pipe through tr ‘[:upper:]’ ‘[:lower:]’ to normalize the case if needed.
You can generate random hex data formatted like a UUID using openssl rand -hex 16, but the result will not have the correct version and variant bits set, so it is not a spec-compliant RFC 4122/9562 UUID. For a properly formatted UUID from the command line, use uuidgen or read directly from /proc/sys/kernel/random/uuid on Linux.