1. Overview

A UUID (Universally Unique Identifier) consists of 32 hexadecimal digits in five groups separated by hyphens. An example UUID is d153e353-2a32-4763-b930-b27fbc980da5.

A UUID is, for practical purposes, unique. That means if we create a UUID, that UUID will be different from all of the created UUIDs in the past, and it will be different from all of the UUIDs created in the future. So the generation of the same UUID, or a collision, is extremely unlikely.

As we know, programming languages support the creation of UUIDs, for example, Java. In this tutorial, we’ll examine creating UUIDs in Bash.

2. Using uuidgen

We can use the uuidgen command for creating UUIDs:

$ uuidgen
fe1982b9-368b-4d60-8a33-ed503224bb78

As we see, uuidgen generated and printed the UUID.

uuidgen can create three types of UUIDs. These are random-based UUIDs, time-based UUIDs and hash-based UUIDs. We’ll discuss each of them.

uuidgen comes installed with the util-linux package.

2.1. Creating Random-Based UUIDs

uuidgen creates a random-based UUID by default if the operating system has a high-quality random number generator. For example, the device /dev/random provides a high-quality randomness.

We can explicitly tell uuidgen to create a random-based UUID using its –r option:

$ uuidgen -r
aefb2b8c-8a22-4f88-904b-0ec3bff176ce

We can use the option –random instead of –r.

2.2. Creating Time-Based UUIDs

The second type of UUID that uuidgen can generate is the time-based UUID. If a high-quality random number generator isn’t available, uuidgen generates a time-based UUID.

We can explicitly specify to create a time-based UUID using the –t option of uuidgen:

$ uuidgen -t
fedf56e2-1723-11ed-bffc-000c2943451c

uuidgen uses the system clock and the MAC address (if there’s an Ethernet card) for generating a time-based UUID.

We can use the option –time instead of -t.

2.3. Creating Hash-Based UUIDs

The third type of UUID that uuidgen can generate is the hash-based UUID. In this case, uuidgen uses cryptographic hash algorithms and provided text strings.

Let’s create a hash-based UUID using uuidgen:

$ uuidgen -m -N www.baeldung.com -n @url
887173cb-5d6e-330f-bff8-38be6ae16a5a

The –m option tells uuidgen to use MD5 as the hash algorithm. We can use –md5 instead of -m.

The –N option specifies the name whose hash shall be generated. The -N www.baeldung.com part of the last command told uuidgen to use the hash value of the string www.baeldung.com while generating the UUID.

The –n option specifies the namespace. uuidgen first appends the namespace given by the -n option to the name given by the -N option and then generates the hash. There are several predefined namespaces, and @url is one of them.

Therefore, the command uuidgen -m -N www.baeldung.com -n @url generated a hash-based UUID using the string www.baeldung.com in the @url namespace as an input to the MD5 hash algorithm.

The other hash algorithm option that we can use is the SHA1 hash algorithm while generating UUIDs. We must use the -s option in this case:

$ uuidgen -s -N www.baeldung.com -n @url
0cd58217-77f5-5a48-acd4-6831afd30ede

We can use –sha1 instead of -s.

3. Using Kernel Generators

Another option is using the read-only file /proc/sys/kernel/random/uuid. A UUID is generated by the kernel’s random number generator every time we get the content of this file, for example using the cat command:

$ cat /proc/sys/kernel/random/uuid
586c074c-9ac2-499a-b795-0d8cdbe8d7b6
$ cat /proc/sys/kernel/random/uuid
e7ca100d-e838-4bdd-b999-97001d8e4318

The UUID generated by the kernel generator is a random-based UUID, just like the one produced with the –r option of uuidgen.

This method for generating a UUID isn’t as portable as using uuidgen as the /proc directory may not be present in every Linux distribution.

4. Using Python

It’s possible to generate UUIDs from the command line using scripting languages. We’ll use Python for this purpose:

$ python –c “import uuid; print(uuid.uuid4())”
80e03405-20d3-4d19-9239-7a6c7256ba61

The –c option passed to the python command lets us execute Python statements from the command line.

The first statement in the double quotes, import uuid, imported the uuid module. Then, we called the uuid4() function of this module using uuid.uuid4(). Finally, we printed the output of this function call, which is the generated UUID, using the statement print(uuid.uuid4()).

The uuid4() function of the uuid module generates a random-based UUID.

Similar to uuidgen, it’s also possible to generate time-based UUIDs. We must use the uuid1() function of the uuid module for this purpose:

$ python –c “import uuid; print(uuid.uuid1())”
a9313fd0-172d-11ed-9557-000c2943451c

Finally, it’s also possible to create UUIDs using MD5 or SHA1 hash algorithms similar to uuidgen. We must use the uuid3() and uuid5() functions in the uuid module for MD5 and SHA1, respectively.

Let’s create a hash-based UUID using MD5:

$ python -c “import uuid; print(uuid.uuid3(uuid.NAMESPACE_URL, ‘www.baeldung.com’))”
887173cb-5d6e-330f-bff8-38be6ae16a5a

We passed uuid.NAMESPACE_URL as the first argument to the function uuid3(). NAMESPACE_URL is the namespace identifier in the uuid module. It corresponds to namespace string, @url, which we passed to uuidgen with the –n option. We passed www.baeldung.com’ as the second argument. That corresponds to the string that we passed to uuidgen using the -N option.

The generated UUID is the same as the one we generated with the command uuidgen -m -N www.baeldung.com -n @url. That’s expected as the inputs are the same.

We can generate a hash-based UUID using SHA1 in the same way:

$ python -c “import uuid; print(uuid.uuid5())”
0cd58217-77f5-5a48-acd4-6831afd30ede

The generated UUID is the same as the one using uuidgen -s -N www.baeldung.com -n @url, as expected.

5. Conclusion

In this article, we discussed three different ways to generate UUIDs in Bash.

First, we learnt how to create random-based, time-based and hash-based UUIDs using the different options of uuidgen.

Second, we used the kernel generator. It only generates random-based UUIDs.

Finally, we saw that it’s possible to create random-based, time-based and hash-based UUIDs using several different functions in Python’ uuid module.