Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In Java i can create a new random UUID with

UUID uuid = UUID.randomUUID();

I now need to do the same within a bash script. Is there a command or library I could use?

share|improve this question

7 Answers 7

up vote 44 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.

share|improve this answer
5  
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 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 at 20:55
1  
@Rob: To answer your original question, it's for drive labels. –  Dennis Williamson Jul 10 at 21:45

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

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

References:

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 at 1:58

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
387ee6b9-520d-4c51-a9e4-6eb2ef15887d

$ dbus-uuidgen
d17b671f98fced5649a856a54b51c9e6

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
3  
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

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:

#!/bin/bash
# ...do some stuff
$myvar = $(perl -e 'use UUID;  UUID::generate($uuid);  UUID::unparse($uuid, $string);  print "$string";')
# ...do 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()'
2d96768e-02b3-11df-bec2-001e68b9d147

Son in the shell:

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

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

share|improve this answer

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
uuid()
{
    local N B T

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

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

        for T in 3 5 7 9
        do
            if (( T == N ))
            then
                printf '-'
                break
            fi
        done
    done

    echo
}

[ "$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 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 at 17:47
apt-get install uuid

Worked for me, then id did run uuid

share|improve this answer

Your Answer

 
discard

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.