How to Generate UUID in Bash [3 Simple Methods]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

UUID (Universal Unique Identifier) is used to uniquely identify almost all entities such as files, digital objects, etc. There are many versions and variants of UUIDs. One can generate UUIDs based on different algorithms. In Bash, you can take various approaches to generate UUID. This article elaborates on those techniques elaborately.

What is UUID?

A UUID (Universal Unique Identifier) is a 128-bit identifier by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE). They are designed to be globally unique across space and time, making them useful for identifying resources or entities in distributed systems without requiring a centralized authority for generation. It consists of 32 hexadecimal digits and is typically divided into 5 groups separated by hyphens like 123e4567-e89b-12d3-a456-426655440000.

Additionally, GUID (Global Unique Identifier) is a variant of UUID used by Microsoft. UUIDs have a couple of variants and versions.

Variant Characteristics
Variant 0 Obsolete.
Variant 1 Mainly used today. These are usually version 1 and version 4 type UUIDs.
Variant 2 Reserved by Microsoft for future use.

3 Ways of Getting UUID in Bash

There are multiple ways to generate UUID in Bash. One can use uuidgen command, system kernel, or even python uuid module to generate UUID. Let’s explore the methods:

Method 1: Using “uuidgen” Command to Generate UUID

The uuidgen command in Bash is super useful to generate UUID in Unix Unix-based system. It offers multiple options such as -r, -t, and -n to generate UUIDs with different randomness based on various algorithms.

Case 1: Creating Random-based UUIDs in Bash

By default, the uuidgen command creates a random-based UUID. However, one can deliberately use -random or -r option to generate random-based UUIDs. Follow the steps to use the method:

  1. At first, launch an Ubuntu Terminal.
  2. Create a file named randomuuid.sh using:
    nano randomuuid.sh
  3. Now, copy-paste the following script into the file.
    #!/bin/bash
    
    uuid=$(uuidgen -r)
    echo "Generated Random-based UUID: $uuid"
    EXPLANATION

    This script utilizes the uuidgen -r command to generate a random-based UUID. The resulting UUID is stored in the uuid variable. Subsequently, the echo command displays the generated UUID in a message.

  4. Press CTRL+S to save the file and CTRL+X to exit.
  5. Use the following command to make the file executable:
    chmod u+x randomuuid.sh

    Changing permission of a Bash script to "generate uuid in bash"

  6. Run the randomuuid.sh script by the following command:
    ./randomuuid.sh

    Generating uuid using uuidgen commandWhen executed, the script generates a random-based UUID as highlighted in the above image.

Case 2: Creating Time-based UUID in Bash

Time-based UUIDs are useful when you need to associate a unique identifier with a specific point in time. -t option of the uuidgen command used to generate time-based UUIDs. Here’s how:

#!/bin/bash

uuid=$(uuidgen -t)
echo "Generated Time-based UUID: $uuid"

Generating time based uuidWhen executed, the program generates a time-based UUID as shown in the image.

Case 3: Creating Hash-based UUID in Bash

You can generate UUIDs using cryptographic hash algorithms like MD5 or SHA1. Here’s how:

#!/bin/bash

echo "Generated UUID using hash algorithm: $(uuidgen -m -N www.linuxsimply.com -n @url)
EXPLANATION

Here -m specifies that the UUID should be generated using a method that combines a name with a namespace. -N enables the command to take the namespace as www.linuxsimply.com. @url indicates that the name is taken from the URL namespace.

 Generating hash based uuidThe script creates a UUID as highlighted in the above image.

Another commonly used hash algorithm is SHA1 algorithm. To generate UUID using hash algorithm SHA1 one must enable the -s option of the uuidgen command. That is:

uuidgen -s -N www.linuxsimply.com -n @url

Method 2: Using Kernel Random Generator to Generate UUID in Bash

One can use the system kernel to generate UUIDs. The /proc/sys/kernel/random/uuid file generates a UUID each time one tries to read the content of this file using a command such as cat. This approach uses the kernel random generator. Here’s a script showing the method:

#!/bin/bash

echo "Random generated UUID: $(cat /proc/sys/kernel/random/uuid)"

Generating uuid using kernel random generatorWhen executed, the program shows a kernel-generated UUID as shown in the image.

NOTE: The process of generating UUID using a kernel generator is the same as generating random-based UUID using uuidgen command.

Method 3: Using Python UUID Module in Bash

If your system has Python installed in it then you can generate UUID using python uuid module. This module offers the uuid4() function that can generate random-based UUID. Here’s how:

#!/bin/bash

uuid=$(python -c "import uuid; print(uuid.uuid4())")
echo "Python Generated UUID: $uuid"
EXPLANATION

The script executes a Python command enclosed in double quotes after python -c. Inside the command, the uuid module is imported, an uuid4() function is called to produce a randomly generated version 4 UUID. The generated UUID is stored in the uuid variable. Finally, the echo command displays the generated UUID.

Generating uuid using python uuid moduleWhen executed, the program shows UUID using the Python uuid module as shown in the image.
  • If Python is not installed in your system, use the command below to install Python:
sudo apt install python-is-python3
  • To generate time based UUIDs use uuid1() function as follows:
python –c "import uuid; print(uuid.uuid1())"
  • One can create UUIDs using MD5 and SHA1 hash algorithms. To create hash-based UUIDs using MD5 algorithm use uuid3() function.
python -c "import uuid; print(uuid.uuid3(uuid.NAMESPACE_URL, 'www.linuxsimply.com'))"

Here, uuid.NAMESPACE_URL is the first argument of the function uuid3() that takes the namespace string to generate UUID.

  • To generate hash-based UUID using SHA1, use the following command:
python -c "import uuid; print(uuid.uuid5())"

Comparing Two UUIDs in Bash

UUIDs are expected to be highly unique, but two UUIDs can appear similar when you clone something. The script below compares two UUIDs to determine whether they are similar or not:

#!/bin/bash

# Generate two UUIDs and store them in an array
uuids=()

for i in {1..2}; do
uuids+=("$(uuidgen)")
done

# Compare the two UUIDs
if [ "${uuids[0]}" = "${uuids[1]}" ]; then
echo "UUIDs are identical."
else
echo "UUIDs are different."
fi
EXPLANATION

The provided Bash script aims to generate two UUIDs using the uuidgen command and a for loop. It then stores them in an array called uuids. After generating the UUIDs, the script compares them using an if statement. It checks whether the values of the first and second elements in the uuids array are equal.  If they are identical, the script outputs a message stating that the UUIDs are identical; otherwise, it prints a message indicating that the UUIDs are different.

Comparing two uuid in BashThe program shows that the two UUIDs it generated within the code are not identical.

Conclusion

In conclusion, there are multiple ways of generating UUIDs in Bash. Moreover, one can choose the algorithm on effect while generating hash-based UUIDs using uuidgen command. The Python uuid module also offers the flexibility to generate UUIDs with different types of randomness. I believe from now on you can generate a UUID in Bash with the quickest amount of time.

People Also Ask

Is there any faster way of genenrating UUID in Bash than uuidgen command?

Generating UUID using uuidgen command is indeed slower when creating a large number of UUIDs. In this case, you can use a kernel random generator to make it a little faster.

Can I create multiple UUIDs at a time?

Yes, you can. Using a for loop with uuidgen command you can generate as many UUIDs you need.

I can not find uuidgen command in my system. What should I do now?

You have to install the uuidgen if it is not available in your system. To install the command in an Ubuntu system run this command apt-get install uuid-runtime in the terminal.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment