Generate a UUID in Bash

Bash is the most common terminal shell and scripting language on Unix-like operating systems (e.g., Linux or macOS).

How to Generate a UUID using Bash

Note: To follow this tutorial, you should have a basic understanding of Bash shell scripting and access to a Unix-like operating system (e.g., Linux or macOS) with a terminal emulator.

There are several ways to generate UUIDs in Bash, and we'll cover three popular methods below.

#1 Using the "uuidgen" Command

The simplest way to generate a UUID in Bash is to use the uuidgen command, which is available on most Unix-based systems.

Open your terminal and type the following command:

uuidgen

This command will generate a random UUID and output it to the console. To store the generated UUID in a variable, use the following syntax:

uuid=$(uuidgen)
echo $uuid

#2 Using the /proc/sys/kernel/random/uuid File

If the uuidgen command is not available on your system, you can use the /proc/sys/kernel/random/uuid file on Linux systems to generate a UUID. This file provides a random UUID each time you read its content.

Here's how to read the UUID from the file:

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

#3 Generating UUIDs with OpenSSL

Another way to generate a UUID is by using the OpenSSL command-line tool, which is commonly available on Unix-based systems.

Use the following command to generate a UUID with OpenSSL:

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

This command generates a 16-byte random number in hexadecimal format, then formats it according to the UUID string representation (8-4-4-4-12).

Conclusion

In this blog post, we discussed three different methods for generating UUIDs using Bash shell scripting. You can choose the most suitable method for your needs, depending on your system's availability of tools like uuidgen or OpenSSL. UUIDs are essential in various applications, and being able to generate them effortlessly in Bash can save you both time and potential collisions in your projects.


How can we improve this page? Let us know!




The UUIDs generated by this site are provided AS IS without warranty of any kind, not even the warranty that the generated UUIDs are actually unique. You are responsible for using the UUIDs and assume any risk inherent to using them. You are not permitted to use the UUIDs generated by this site if you do not agree to these terms. Do not use any UUIDs found on cached versions of this page.
Privacy Policy
This website uses cookies. We use cookies to personalise content/ads and to analyse our traffic.

The UUIDs generated by this site conform to RFC 4122 whenever possible.
Read more about UUIDs at Wikipedia.
Check out our developer API.

Copyright © 2024 TransparenTech LLC. All Rights Reserved. Contact Us.