Generate UUID in Shell

Table of Contents

Method 1: Using the uuidgen command

#!/bin/bash

uuid=$(uuidgen)
echo "Generated UUID: $uuid"

Method 2: Using the openssl command

#!/bin/bash

uuid=$(openssl rand -hex 16)
echo "Generated UUID: $uuid"

Method 3: Using the cat command and /proc/sys/kernel/random/uuid

#!/bin/bash

uuid=$(cat /proc/sys/kernel/random/uuid)
echo "Generated UUID: $uuid"

Method 4: Using the uuid command-line tool

#!/bin/bash

uuid=$(uuid)
echo "Generated UUID: $uuid"

Summary

In this tutorial, we explored four different ways to generate UUIDs in shell scripts. We learned how to use the uuidgen command, the openssl command, the cat command with /proc/sys/kernel/random/uuid, and the uuid command-line tool. Each method provides a simple way to generate unique identifiers suitable for various use cases.

References

  1. UUIDgen - The uuidgen command-line tool. (2021, August 10). Retrieved from https://www.man7.org/linux/man-pages/man1/uuidgen.1.html
  2. OpenSSL Documentation. (n.d.). Retrieved from https://www.openssl.org/docs/