LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-07-2003, 09:47 AM   #1
LQ Newbie
Registered: May 2003
Posts: 5

Rep: Reputation: 0
Question creating a unique id string in shell

How would I generate a unique id string within a Bash shell? PHP has a nice function called uniquid() that generates a 13 digit unique string. Any suggestions for how to do this in Linux shell scripting? I found random(), but was unsure how to use it. Thanks for any assistance.
Old 05-07-2003, 01:49 PM   #2
Registered: May 2001
Posts: 28,886
Blog Entries: 55

Rep: Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356
r=( $(openssl rand 100000 | sha1sum) ); printf "%s${r[0]:0:13}\n"

If you haven't got|don't want OpenSSL, and you can't|won't use designated devices like /dev/random or the Entropy Gathering Daemon then try using data that changes a lot, like catting disk/network stats from /proc, time, etc etc...
Old 05-07-2003, 01:56 PM   #3
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
You can use the $RANDOM shell variable, but it is not guaranteed to be unique. Not sure what you need it for, but if is for temporary file names you could try mktemp.
Old 05-07-2003, 04:42 PM   #4
Registered: Jan 2002
Posts: 52

Rep: Reputation: 15
I don't know of a solution, but search Google for a GUID generator. You might find one that you can port to the language/platform of your choice and then execute it from the within the shell.
Old 05-07-2003, 06:56 PM   #5
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Re: creating a unique id string in shell

Originally posted by gumby
How would I generate a unique id string within a Bash shell?
- If it doen't need to be random, but just unique, how about a simple counter? Increase by 1 each time you need another identifier, padding with zero's if you need the id's to be fixed length strings.

- If you just need one unique id for each instance of the script, how about it's process id? You get it (in bash/sh) easily with the $$-variable, e.g. "echo $$".

(or maybe I didn't get your question right?)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - String Replacement revof11 Programming 7 11-29-2005 07:38 AM
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 05:49 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 10:23 AM
string shell command? minm Linux - Newbie 10 08-19-2004 11:09 AM
Removing Text from a Shell String OrangeLefty Programming 2 04-19-2004 12:08 AM

All times are GMT -5. The time now is 01:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration