Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Java it is possible to create a random UUID:

UUID uuid = UUID.randomUUID();

How to do this in Bash?

share|improve this question

10 Answers 10

up vote 89 down vote accepted

See the uuidgen program which is part of the e2fsprogs package.

According to this, libuuid is now part of util-linux and the inclusion in e2fsprogs is being phased out.

(On new Ubuntu systems, uuidgen is now in the uuid-runtime package.)

To create a uuid and save it in a variable:


On my Ubuntu system, the alpha characters are output as lower case and on my OS X system, they are output as upper case (thanks to David for pointing this out in a comment).

To switch to all upper case (after generating it as above):


To switch to all lower case:


If, for example, you have two UUIDs and you want to compare them in Bash, ignoring their case, you can do a tolower() style comparison like this:

if [[ ${uuid1,,} == ${uuid2,,} ]]
share|improve this answer
hey, no fair! my e2fsprogs didn't come with that! i want one, where do i get it? (update: ahhh... debian sticks it in the uuid-runtime package for no apparent reason... +1 to you) – quack quixote Jan 16 '10 at 14:05
uuidgen comes builtin on freeBSD. it isn't always in the e2fsprogs package. – Good Person May 22 '14 at 1:58
EDIT: Just read the last line. I'm a dork. Please carry on..... Be interested to hear why this is bundled in e2fsprogs, as this doesn't really seem that much like a filesystem-specific tool to me. – Rob Jul 10 '14 at 20:55
@Rob: To answer your original question, it's for drive labels. – Dennis Williamson Jul 10 '14 at 21:45
I notice uuidgen on Mac emits in all uppercase while on Ubuntu (uuidgen from util-linux 2.20.1) in all lowercase. Why the difference? Also Ubuntu listed where the tool came from but on Mac, there's no version info nor which package it came from. – David Oct 14 '15 at 18:27

To add variety without adding external dependencies, on Linux you can do:

UUID=$(cat /proc/sys/kernel/random/uuid)

To propagate bad practices, on FreeBSD, under the linux compatibility layer (linuxulator?),

UUID=$(cat /compat/linux/proc/sys/kernel/random/uuid)


share|improve this answer
This is awesome. – Tom O'Connor Aug 7 '13 at 13:55
This should be avoided as it is highly non-portable (although FreeBSD supplies /compat/linux/proc/sys/kernel/random/uuid for poorly written applications) – Good Person May 22 '14 at 1:58
It fits perfectly for usage inside of initrd image – Maximilian Jun 4 '15 at 16:08
The FreeBSD method doesn't appear to be on Mac OS X (nor the Linux method). At least uuidgen appears to be preinstalled on the Mac (or was installed with something else I installed). – David Oct 14 '15 at 18:25
@David you are absolutely right. The FreeBSD method is for FreeBSD, and the Linux method is for Linux, as mentioned in the answer. Any bash script that need to run on all 3 systems (FreeBSD, Linux, MacOS) will need to check against uname -s and use the appropriate method. – Samveen Oct 15 '15 at 6:48

Just for the sake of completeness... There's also a UUID generator installed with the dbus package on Debian. I missed it looking around earlier. It's probably the same algorithm as the e2fsprogs package, but it doesn't add the dashes, so it might be a little cleaner for you:

$ uuidgen

$ dbus-uuidgen

Grawity adds a safety tip: "DBus UUIDs are not related to or compatible with RFC 4122. Besides, dbus-uuidgen always uses the Unix timestamp as the last 4 bytes. So they might be unsuitable for some uses." (Thanks, Grawity, I should've spotted that in the manpage.)

share|improve this answer
DBus UUIDs are not related to or compatible with RFC 4122. Besides, dbus-uuidgen always uses the Unix timestamp as the last 4 bytes. So they might be unsuitable for some uses. – grawity Jan 16 '10 at 15:08

If you do not want to depend on other executables, or you cannot use them, here is the pure bash version from here:

# Generate a pseudo UUID
    local N B T

    for (( N=0; N < 16; ++N ))
        B=$(( $RANDOM%255 ))

        if (( N == 6 ))
            printf '4%x' $(( B%15 ))
        elif (( N == 8 ))
            local C='89ab'
            printf '%c%x' ${C:$(( $RANDOM%${#C} )):1} $(( B%15 ))
            printf '%02x' $B

        for T in 3 5 7 9
            if (( T == N ))
                printf '-'


[ "$0" == "$BASH_SOURCE" ] && uuid
share|improve this answer
The T variable can be eliminated and the for T loop can be changed to: case $N in 3 | 5 | 7 | 9) printf '-';; esac (broken out on separate lines if preferred). – Dennis Williamson May 22 '14 at 14:08
I added a comment to the code at the github link showing a version using case to eliminate the if statements as well as the inner for statement. It makes the code much neater. Note that both B%15 should be B%16 and B%255 should be B%256. – Dennis Williamson May 22 '14 at 17:47

Perl provides a UUID library based on the e2fsprogs package. On my Debian system it's the libuuid-perl package. Here's an example one-liner; see man uuid for more:

$ perl -e 'use UUID;  UUID::generate($uuid);  UUID::unparse($uuid, $string);  print "my new UUID is $string \n";'
my new UUID is 3079e9ce-41d4-4cf3-9f90-d12f8bb752e4

This would be trivial to add to a shellscript with backticks or $() notation:

# some stuff
$myvar = $(perl -e 'use UUID;  UUID::generate($uuid);  UUID::unparse($uuid, $string);  print "$string";')
# some more stuff
share|improve this answer
+1 - Help me a lot! – rafa.ferreira Feb 16 '11 at 13:29

Just so python doesn't feel left out:

python  -c 'import uuid; print uuid.uuid1()'

To use it in the shell:

myvar=$(python  -c 'import uuid; print uuid.uuid1()')

See the Python Documentation UUID for the kinds of UUIDS that can be generated.

share|improve this answer
apt-get install uuid

Worked for me, then id did run uuid

share|improve this answer

I wrote a little Bash function using Python to generate an arbitrary number of UUIDs in bulk:

# uuid [count]
# Generate type 4 (random) UUID, or [count] type 4 UUIDs.
function uuid()
    local count=1
    if [[ ! -z "$1" ]]; then
        if [[ "$1" =~ [^0-9] ]]; then
            echo "Usage: $FUNCNAME [count]" >&2
            return 1


    python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))'

If you prefer lowercase, change:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))'


python -c 'import uuid; print("\n".join([str(uuid.uuid4()) for x in range('"$count"')]))'
share|improve this answer

Please look at the OSSP UUID library (, and consider installing it. Some projects offer it as an option (e.g. PostgreSQL). It properly handles version 3 and version 5 UUIDs, which was beyond what my installed (e.g. e2fsprogs) library could handle. Fortunately, openSUSE has it in one of the main repos. Getting a version to work w/ Windows (e.g. Cygwin) or MySQL has been a flail. Looks like it is time to switch to Linux/PostgreSQL/Python (and I so loved the SQLyog GUI to MySQL/MariaDB) since I really need v3 and v5 UUIDs.

share|improve this answer
Agree completely! For my use case it was perfect as it also supports namespacing via the -v3 ns:URL custom-data seeding mechanism. – Roberto Andrade Oct 6 '15 at 18:09

I'm sure some will arrive here, and are just looking for an easy way to generate a unique ID for use in their scripts, and it doesn't need to be a true UUID.

If so, you can just do:

MYID="U$(date +%s)"
echo $MYID

will generate ids like the following based off the current system time:


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.