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:
- At first, launch an Ubuntu Terminal.
- Create a file named randomuuid.sh using:
nano randomuuid.sh
- Now, copy-paste the following script into the file.
#!/bin/bash uuid=$(uuidgen -r) echo "Generated Random-based UUID: $uuid"
EXPLANATIONThis script utilizes the
uuidgen -r
command to generate a random-based UUID. The resulting UUID is stored in theuuid
variable. Subsequently, the echo command displays the generated UUID in a message. - Press CTRL+S to save the file and CTRL+X to exit.
- Use the following command to make the file executable:
chmod u+x randomuuid.sh
- Run the randomuuid.sh script by the following command:
./randomuuid.sh
When 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"
When 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)
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.
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. 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)"
When executed, the program shows a kernel-generated UUID as shown in the image.
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"
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.
- 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
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.
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
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners