How to Generate UUID in Bash [3 Simple Methods]

UUID or Universal Unique Identifier is used to uniquely identify almost all the 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 different approaches to generate UUID. This article discusses those techniques elaborately.

Key Takeaways

  • Understanding about UUID.
  • UUIDs version and variants.
  • Techniques of generating UUIDs in Bash.

Free Downloads

What is UUID?

A UUID or Universally Unique Identifier is a 128-bit identifier that is almost guaranteed to be unique until the year 3400. It can identify almost anything imaginable on the internet including databases, Bluetooth devices and objects within a short lifetime. A UUID sequence of 32 hexadecimal digits is typically divided into five groups separated by hyphens, like below- 123e4567-e89b-12d3-a456-426655440000.

GUID in other words Global Unique Identifier is actually 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. I am going to discuss these techniques below.

You can read the Comparative Analysis of Methods to find one that suits you.

Method 1: Using uuidgen Command to Generate UUID

The uuidgen command in Bash is super useful to generate UUID in 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 random-based UUID. However, one can deliberately use -random or -r option to generate random-based UUIDs. This provides high-quality randomness of the generated UUID.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file named randomuuid.sh in the build-in nano editor:

nano randomuuid.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • randomuuid.sh: Name of the file.
Creating Bash script in nano❸ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

Script (randomuuid.sh) >

#!/bin/bash

uuid=$(uuidgen -r)
echo "Generated Random-based UUID: $uuid"
EXPLANATION

This script utilizes the uuidgen command with the -r option 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.

❹ Use the following two commands to make both file executable:

chmod u+x randomuuid.sh
EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • randomuuid.sh: Name of the script.
Changing permission of a Bash script to "generate uuid in bash"❺ 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.

You can follow the Steps of Case 1 to learn about creating and saving shell scripts.

Script (timeuuid.sh) >

#!/bin/bash

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

This program utilizes the uuidgen command with the -t option 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.

Run the timeuuid.sh script by the following command:

./timeuuid.sh

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

One can use uuidgen command to generate UUIDs using cryptographic hash algorithms like MD5 or SHA1.

You can follow the Steps of Case 1 to learn about creating and saving shell scripts.

Script (hashuuid.sh) >

#!/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.

Run the hashuuid.sh script by the following command:

./hashuuid.sh

 Generating hash based uuidOnce executed the 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. An example command can be like this-

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.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (kernel.sh) >

#!/bin/bash

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

This Bash script outputs a UUID by reading from the /proc/sys/kernel/random/uuid file. Then the UUID is displayed using the echo command.

Run the kerneluuid.sh script by the following command:

./kernel.sh

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 kernel generator is 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  Universal Unique Idenfier.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (pythonuuid.sh) >

#!/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.

Run the pythonuuid.sh script by the following command:

./pythonuuid.sh

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())"

Comparative Analysis of Methods

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your need.

Method Advantage Disadvantage
uuidgen command
  • Flexible to generate

UUID with different type of randomness.

  • Slow in implementation.
Kernel Generator
  • Generate

UUID by reading /proc/sys/kernel/random/uuid this file.

  • May not be available.
Python uuid module
  • Flexible to generate

UUID of different visions.

  • May not be installed in the system.

From personal experience, I would suggest you to use the uuidgen command to generate UUID. This command is flexible and generates UUID with higher order randomness.

Comparing two UUIDs in Bash

UUIDs are expected to be highly unique, but two UUIDs can appear similar when you clone something. In the script below, I will create a Bash script that can compare two UUIDs to determine whether they are similar or not:

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (compare.sh) >

#!/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.

Run the compare.sh script by the following command:

./compare.sh

Comparing two uuid in BashOnce executed the program shows that 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?
It is true that generating UUID using uuidgen command is slower when creating large number of UUIDs. In this case, you can use kernel random generator to make it 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 is 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