How to Generate a UUID in Bash
The standard way to generate a UUID in Bash is the uuidgen command, pre-installed on macOS (part of libuuid) and most Linux distributions (part of util-linux). Call it directly or capture the output in a variable for use elsewhere in your script. The r uuid reference explains how to use uuid::UUIDgenerate() for random v4 identifiers in R.
#!/bin/bash
my_uuid=$(uuidgen)
echo "$my_uuid"
# Output: 550E8400-E29B-41D4-A716-446655440000
# Normalize to lowercase (uuidgen prints uppercase on macOS, lowercase on most Linux)
echo "$my_uuid" | tr '[:upper:]' '[:lower:]'
Explanation
uuidgenships as part of util-linux on Linux and libuuid on macOS, so it's available on almost every Unix-like system with no extra install.my_uuid=$(uuidgen)captures the generated identifier in a shell variable using command substitution.- macOS's
uuidgentypically prints uppercase hex digits while most Linux distributions print lowercase — pipe throughtr '[:upper:]' '[:lower:]'to normalize the case before storing or comparing UUIDs. - With no flags,
uuidgendefaults to generating a random (version 4) UUID.
Validate a UUID String in Bash
Bash has no typed UUID value, so validating a UUID string means checking that it matches the expected shape with a regular expression. The [[ ... =~ ... ]] conditional performs the match without spawning an external process. For idiomatic Kotlin UUID patterns with extension functions and sealed classes, see the kotlin uuid guide.
#!/bin/bash
id="550e8400-e29b-41d4-a716-446655440000"
pattern='^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
if [[ "$id" =~ $pattern ]]; then
echo "valid"
else
echo "invalid"
fi
Explanation
- The
patternvariable holds a regex matching the standard 36-character UUID shape: 8-4-4-4-12 hexadecimal groups separated by hyphens. [0-9a-fA-F]matches both uppercase and lowercase hex digits directly, so there's no need to enableshopt -s nocasematchor normalize the string's case first.[[ "$id" =~ $pattern ]]performs the match; storing the pattern in a variable rather than inlining it avoids quoting issues with the regex's special characters.- This only checks the string's shape, not its version or variant bits — a string like
00000000-0000-0000-0000-000000000000still passes.
Other Options for Generating UUIDs in Bash
uuidgen also accepts flags for forcing a specific UUID version, and Linux offers a kernel-level fallback for systems where uuidgen isn't installed.
#!/bin/bash
# Force a random (version 4) UUID
uuidgen -r
# Force a time-based (version 1) UUID
uuidgen -t
# Kernel-level fallback on Linux -- no package required
cat /proc/sys/kernel/random/uuid
Explanation
uuidgen -rexplicitly requests a random, version 4 UUID — useful when you want to be certain of the version regardless of the system default.uuidgen -tforces a time-based, version 1 UUID, which encodes the current timestamp and a node identifier instead of pure randomness.- On Linux,
cat /proc/sys/kernel/random/uuidreads a freshly generated random UUID straight from the kernel — it works even on minimal or containerized systems whereuuidgenisn't installed. - The kernel-random-uuid approach is Linux-specific; it has no equivalent on macOS, so fall back to
uuidgenthere.