Skip to content

Generate UUID in Bash

The easiest way to generate a random UUID version 4 in Bash is using a tool called uuidgen

bash
uuidgen
#=> 4fdeff35-9e1e-4efa-a67c-a248d0043708

You can also generate versions 1, 3 and 5 with this tool, using the appropriate parameters:

bash
# generate UUID version 1 - time based
uuidgen --time

# generate UUID version 3 - deterministic and hashed with MD5
uuidgen --name name --namespace `uuidgen` --md5

# generate UUID version 4 - random
uuidgen

# generate UUID version 5 - deterministic and hashed with SHA-1
uuidgen --name name --namespace `uuidgen` --sha1

Installation & Setup

Most Linux distributions include uuidgen by default:

bash
# Check if uuidgen is installed
which uuidgen

# Install on Ubuntu/Debian if missing
sudo apt-get install uuid-runtime

# Install on CentOS/RHEL if missing
sudo yum install util-linux

Advanced Bash Usage

Generate multiple UUIDs:

bash
#!/bin/bash

# Generate 10 UUIDs
for i in {1..10}; do
    echo "UUID $i: $(uuidgen)"
done

# Generate UUIDs with timestamp
echo "$(date): $(uuidgen)" >> uuid_log.txt

Validate UUID format:

bash
#!/bin/bash

validate_uuid() {
    local uuid=$1
    if [[ $uuid =~ ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$ ]]; then
        echo "Valid UUID: $uuid"
        return 0
    else
        echo "Invalid UUID: $uuid"
        return 1
    fi
}

# Usage
validate_uuid $(uuidgen)

UUID Version Comparison

Choose the right version for your Bash scripts:

  • Version 1 - Time-based, includes MAC address
  • Version 3 - MD5 namespace-based, deterministic
  • Version 4 - Random, most popular choice
  • Version 5 - SHA-1 namespace-based, more secure than v3
  • Version 6 - Time-ordered, better than v1 for databases
  • Version 7 - Modern time-based with improved sorting

For Bash scripting:

  • Log files: Use Version 4 for unique log entry identification
  • Temporary files: Consider Version 1 for time-based cleanup
  • Build systems: Use Version 5 for deterministic artifact naming

How do I generate UUID in other languages?

Scripting and automation:

  • Python - Advanced scripting and automation
  • Groovy - JVM-based scripting

System development:

  • JavaScript - crypto.randomUUID() & uuid library
  • Java - java.util.UUID & UuidCreator
  • Go - google/uuid package
  • Rust - uuid crate

← Back to Online UUID Generator