t_sz3q
Last Updated: December 01, 2017
·
14.94K
· lorin
Me pixel

Generate UUID at shell prompt

I often need to generate uuids. Here's an alias in my ~/.profile that will generate a uuid, show it on the terminal, and copy it to the OSX clipboard:

alias uuid="python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)' | pbcopy && pbpaste && echo"

Works like this:

$ uuid
34469137412242129cd908e384717794

Philip Durbin (@philipdurbin) and Matthew Deiters (@mdeiters) both pointed out that I could have used uuigen instead. Here's what the same thing would like using uuidgen.

alias uuid="uuidgen | tr -d - | tr -d '\n' | tr '[:upper:]' '[:lower:]'  | pbcopy && pbpaste && echo"

I don't like how uuidgen generates hyphens and uppercase letters, so I use tr to remove the hyphen and convert to lowercase. I also remove the trailing '\n' that uuidgen adds because I'm typically copy-pasting a uuid into a quoted strong.

Say Thanks
Respond

5 Responses
Add your response

4922
12003957 10206752435102297 2524621803006121539 n

another nice trick on OSX is the command line utility uuidgen

over 1 year ago ·
4931
Me pixel

@mdeiters Forgot about the existence of uuidgen, I edited the tip.

over 1 year ago ·
4945
81609937c20aeab3598aea56f1f4f022

Try this =>
uuidgen | tr '-' nil | tr '[:upper:]' '[:lower:]'

over 1 year ago ·
4947
Dot

@lorin tr lets you specify a set of characters with -d, so this is a bit shorter:

uuidgen | tr -d '\n-' | tr '[:upper:]' '[:lower:]'

over 1 year ago ·
25934
None

Remove the \n from the argument for -d and the output will be displayed on its own line and not appended to the terminal prompt that's displayed after the operations finish, just as with uuidgen used on its own.

over 1 year ago ·
Filed Under

Awesome Job

E20ce6a2 ca1f 11e7 82be 7f617c8310c9
SENIOR DEVELOPER (m/w)
·
Berlin, Köln, Hamburg, München
·
Full Time