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. However, on new Ubuntu systems, uuidgen is now in the uuid-runtime package.
To create a uuid and save it in a variable:
uuid=$(uuidgen)
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):
uuid=${uuid^^}
To switch to all lower case:
uuid=${uuid,,}
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:
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:
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.)
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
I have found this script "one-liner" useful where uuidgen is not available. This also bypasses any neccessity to install external modules for Perl or Python.
od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}'
Tested on SnowLeopard, Red Hat Valhalla, Solaris 9 4/04 and newer successfully. I am curious if this is prone to non-uniqueness, but I have not been 'bit'ten in the last 10 years. Of course, head -1 could be replaced with head -_other-value_ | tail -1 too.
To explain,
/dev/random and /dev/urandom are kernel random generators.
od (octal dump) has a hex output switch (-x) producing 16 bytes per line.
head -n [| tail -1] (where n>0) extracts just one line of the previous output.
awk sets the OutputFieldSeparator to be a hyphen everywhere a comma occurs in the print statement. By specifying fields 2-9 independently, we control the hyphens and strip off the index/offset counter that 'od' prefixes each line of output with.
The result is a pattern of 8-4-4-4-12 lower case characters a-f0-9.
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
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
fi
count="$1"
fi
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"')]))'
To:
python -c 'import uuid; print("\n".join([str(uuid.uuid4()) for x in range('"$count"')]))'
Please look at the OSSP UUID library (http://www.ossp.org/pkg/lib/uuid/), 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.
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. However, on new Ubuntu systems,uuidgen
is now in theuuid-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:To add variety without adding external dependencies, on Linux you can do:
To propagate bad practices, on FreeBSD, under the linux compatibility layer (linuxulator?),
References:
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: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.)
If you do not want to depend on other executables, or you cannot use them, here is the pure bash version from here:
I have found this script "one-liner" useful where uuidgen is not available. This also bypasses any neccessity to install external modules for Perl or Python.
Tested on SnowLeopard, Red Hat Valhalla, Solaris 9 4/04 and newer successfully. I am curious if this is prone to non-uniqueness, but I have not been 'bit'ten in the last 10 years. Of course,
head -1
could be replaced withhead -_other-value_ | tail -1
too.To explain,
/dev/random
and/dev/urandom
are kernel random generators.od
(octal dump) has a hex output switch (-x) producing 16 bytes per line.head
-n [| tail -1] (where n>0) extracts just one line of the previous output.awk
sets the OutputFieldSeparator to be a hyphen everywhere a comma occurs in the print statement. By specifying fields 2-9 independently, we control the hyphens and strip off the index/offset counter that 'od' prefixes each line of output with.The result is a pattern of
8-4-4-4-12
lower case charactersa-f0-9
.Just so python doesn't feel left out:
To use it in the shell:
See the Python Documentation UUID for the kinds of UUIDS that can be generated.
To generate a systemd machine-id compatible file on a non-systemd machine, you could use python to do it this way:
Perl provides a UUID library based on the
e2fsprogs
package. On my Debian system it's thelibuuid-perl
package. Here's an example one-liner; seeman uuid
for more:This would be trivial to add to a shellscript with backticks or
$()
notation:Worked for me, then i did run
uuid
I wrote a little Bash function using Python to generate an arbitrary number of UUIDs in bulk:
If you prefer lowercase, change:
To:
Please look at the OSSP UUID library (http://www.ossp.org/pkg/lib/uuid/), 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.