Generating UUID in Bash

Bash is a Unix shell and command language, which serves as an enhanced version of the Bourne Shell (sh). As the default shell for most Linux distributions and Unix systems, Bash is renowned for its powerful scripting capabilities and high level of environmental control. Bash scripts can perform a variety of tasks, ranging from simple file operations to complex system administration tasks. Features of Bash include command line argument expansion, piping, redirection, pattern matching, and command substitution, making Bash highly flexible when dealing with files and text.

Bash also supports advanced programming structures such as functions, arithmetic operations, command grouping, loops, and conditional statements, enabling the easy implementation of complex automation tasks. Another important feature of Bash is its extensive support for environment variables and shell script variables, allowing users to customize and optimize their working environment. The extensibility of Bash is also evident in its ability to utilize external commands and programs, as well as interact with other programming languages through shell scripts.

1. The Native uuidgen Utility

The uuidgen utility represents the most streamlined approach to UUID generation. This built-in tool ensures compliance with UUID standards while maintaining implementation simplicity:

uuidgen

For programmatic use, you can capture the output in a variable:

uuid=$(uuidgen)
echo $uuid

2. Kernel-Based Random Generation

This approach leverages Linux's kernel-level random number generator, offering a robust solution that draws from the system's entropy pool. Access is achieved through a dedicated system file:

Copyuuid=$(cat /proc/sys/kernel/random/uuid)
echo $uuid

3. Cryptographic Generation with OpenSSL

The OpenSSL-based approach provides a cryptographically secure method for UUID generation. It generates a random byte sequence and formats it according to UUID specifications:

Copyuuid=$(openssl rand -hex 16)
echo ${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}

4. Using tr and dd Commands

This method uses /dev/urandom to generate random data, then pipes it to the tr command to filter characters, retaining only hexadecimal characters (a-f0-9), followed by fold to wrap the output into 32-character wide lines, and finally head to take the first line as the UUID.

uuid=$(cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 32 | head -n 1)
echo $uuid
  • cat /dev/urandom: Outputs random data.
  • tr -dc 'a-f0-9': Deletes all characters except hexadecimal characters.
  • fold -w 32: Wraps each line of input text to a width of 32.
  • head -n 1: Takes only the first line as output.

5. Using od and awk Commands

This method uses the od command to convert random data into hexadecimal representation and then formats it into UUID format with the awk command.

od -An -N 16 -tx1 /dev/urandom | awk '{printf "%s%s%s%s-%s-%s-%s-%s%s%s\n", substr($1,3,4), substr($1,2,4), substr($1,1,4), substr($2,3,4), substr($2,2,4), substr($3,1,2), substr($2,1,2), substr($3,2,2), substr($3,3,2)}'
  • od -An -N 16 -tx1 /dev/urandom: Outputs 16 bytes of random data in hexadecimal format.
  • awk '{printf "..."}': Uses awk's printf function and substr function to format the string, creating the various parts of the UUID and inserting hyphens in the appropriate places.

6.Using base64 and dd Commands

This method combines base64 encoding with dd command to generate random data, then filters out hexadecimal characters with tr and formats it into a UUID.

uuid=$(cat /dev/urandom | base64 | head -c 16 | tr -dc 'a-f0-9' | fold -w 2 | head -n 5 | tr -d '\n' | sed 's/\(.\)\(.\)/\1-\2/g;s/-/4-/g;s/-.\{3\}$//')
echo $uuid
  • cat /dev/urandom: Outputs random data.
  • base64: Encodes the random data into base64 format.
  • head -c 16: Takes the first 16 characters as the core part of the UUID.
  • tr -dc 'a-f0-9': Deletes all characters except hexadecimal characters.
  • fold -w 2: Wraps every two hexadecimal characters into pairs.
  • head -n 5: Takes the first 5 lines since a UUID requires 5 sets of characters.
  • tr -d '\n': Deletes newline characters.
  • sed 's/(.)(.)/\1-\2/g;s/-/4-/g;s/-.{3}$//': Uses sed commands to insert hyphens, add a '4' before the fourth group, and finally remove the last hyphen.