How to Use the macOS uuidgen Command to Generate Unique Identifiers

When engineering software on macOS, writing robust bash scripts, or provisioning devices via an MDM (Mobile Device Management) platform like Jamf, you often need to generate mathematically unique strings of text. These strings are used as database keys, configuration profile identifiers, or temporary file names to prevent naming collisions. While you could attempt to generate a random number, the mathematical probability of two random numbers colliding is too high for enterprise production environments. To solve this, macOS engineers rely on the uuidgen command to construct universally unique identifiers directly from the Terminal.

Why Use the uuidgen Command?

A UUID (Universally Unique Identifier) is a 128-bit label formatted as a string of 32 hexadecimal characters, grouped by hyphens (for example: 123e4567-e89b-12d3-a456-426614174000). The uuidgen command leverages the underlying macOS kernel architecture to output a Version 4 UUID. This generation process relies on the system’s cryptographic random number generator, ensuring that the resulting mathematical string is so statistically unique that it can be considered globally exclusive without requiring a central registration authority. This is a strict requirement when building .mobileconfig XML profiles for enterprise fleet deployment.

Step 1: Generate a Standard UUID

Generating a single UUID requires no complex syntax.

  1. Open the macOS Terminal (located in Applications > Utilities).
  2. Type the command:
uuidgen
  1. Press Enter.
  2. The terminal will immediately print a mathematically unique string, such as E6B202D1-B738-4A92-A3E5-A8F53B75B463. Notice that by default, the macOS utility outputs the hexadecimal characters in uppercase.

Step 2: Generate Multiple Identifiers

If you are writing a script that creates a complex configuration profile, you will need a unique identifier for the main PayloadUUID, as well as a separate, unique identifier for every single sub-component array inside the profile.

You can execute the command multiple times in sequence:

uuidgen; uuidgen; uuidgen

The terminal will instantly print three completely distinct strings on separate lines, guaranteeing mathematical isolation between the payloads.

Step 3: Assign a UUID to a Variable in Bash

The true power of uuidgen is realized when it is injected dynamically into shell scripts.

If you are writing a script that generates temporary log files and you must ensure the file names never overwrite each other, you can capture the output of the command mathematically.

  1. In your bash script, use command substitution (the $(...) syntax) to assign the output to a variable:
TEMP_FILE=\"/tmp/log_$(uuidgen).txt\"
echo \"Process completed\" > $TEMP_FILE

When the script executes, it will evaluate uuidgen, capture the unique string, and mathematically bind it to the file path (e.g., /tmp/log_9C381E-....txt). This eliminates the risk of race conditions where two simultaneous script executions attempt to write to the exact same file.

By mastering the uuidgen command, macOS administrators can programmatically enforce absolute data isolation and strictly adhere to Apple’s XML schema requirements for configuration profiles.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.