How to generate UUID in Bash

UUID, or Universally Unique Identifiers, are 128-bit numbers that are used to uniquely identify objects or entities in computer systems. They are widely used in various applications such as database record identifiers, network protocols, and software components. In this article, we will show you how to generate UUID in Bash using various methods, including the uuidgen command and using the /proc/sys/kernel/random/uuid file.

Method 1: Using the uuidgen command

The uuidgen command is a built-in command in Linux and macOS systems that generates a random UUID. To generate a UUID using this command, simply open your terminal and type the following command:

$ uuidgen

This will output a randomly generated UUID in the standard UUID format, which looks like this:

b2f58bcf-1e89-43ec-8192-98b54e730237

You can use the -r option to generate a UUID using a random number generator instead of the default method:

$ uuidgen -r

This will output a randomly generated UUID (version-4) in the standard UUID format, which looks like this:

aaec97bf-8032-4261-a7ec-0de22314cfa1

You can also use the -t option to generate a UUID version-1 based on the current timestamp and the MAC address of your network interface:

$ uuidgen -t

The result will be a UUID version-1 in the standard UUID format, looking like this:

a94d8e80-ca9a-11ed-b5fc-51749f1e347c

Method 2: Using Kernel Generators

If you're on a system that doesn't have the uuidgen command installed, you can still generate a UUID using the cat command and the /proc/sys/kernel/random/uuid file. This file contains a randomly generated UUID that you can read using the cat command. To generate a UUID using this method, open your terminal and type the following command:

$ cat /proc/sys/kernel/random/uuid

This will output a randomly generated UUID (v4) in the standard UUID format, just like the uuidgen command.

import { v4 as uuidv4 } from 'uuid';