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.
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.
❶ 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
- nano: Opens a file in the Nano text editor.
- randomuuid.sh: Name of the file.
Script (randomuuid.sh) >
#!/bin/bash
uuid=$(uuidgen -r)
echo "Generated Random-based UUID: $uuid"
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
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- randomuuid.sh: Name of the script.
./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.
Script (timeuuid.sh) >
#!/bin/bash
uuid=$(uuidgen -t)
echo "Generated Time-based UUID: $uuid"
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
When 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.
Script (hashuuid.sh) >
#!/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.
Run the hashuuid.sh script by the following command:
./hashuuid.sh
Once 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.
Script (kernel.sh) >
#!/bin/bash
echo "Random generated UUID: $(cat /proc/sys/kernel/random/uuid)"
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
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 Universal Unique Idenfier.
Script (pythonuuid.sh) >
#!/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.
Run the pythonuuid.sh script by the following command:
./pythonuuid.sh
When 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 |
UUID with different type of randomness. |
|
Kernel Generator |
UUID by reading /proc/sys/kernel/random/uuid this file. |
|
Python uuid module |
UUID of different visions. |
|
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:
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
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
Once 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
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