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

The above is not working..How can I read random bytes from /dev/urandom in bash?

share|improve this question

4 Answers 4

up vote 13 down vote accepted
random="$(dd if=/dev/urandom bs=3 count=1)"
share|improve this answer
    
@Flimzy,how does $() work? –  linux Jun 23 '11 at 6:07
2  
It treats the output of a command like a variable. Note: That's a bashism. If you're not using bash, you may need to use `` instead. `` is more universal, but I think $() is easier to read. –  Flimzy Jun 23 '11 at 6:08
    
@Flimzy,It treats the output of a command like a variable.,you actually mean as string,right? –  linux Jun 23 '11 at 6:14
2  
A variable can be a string... or a number. It treats it as a variable... then depending on context, it's treated like a string, or a number. –  Flimzy Jun 23 '11 at 6:27
    
$() is pretty universal, not bash-specific. BTW, be careful you don't run into limitations on what characters the shell can store in variables -- for example, the version of bash I tested with leaves out nulls (\x00) from the string. –  Gordon Davisson Jun 23 '11 at 13:13
head -c 500 /dev/urandom | tr -dc 'a-zA-Z0-9~!@#$%^&*_-' | fold -w 3 | head -n 1

(If you want literal dash characters, the dash character must go at the end of the string).

share|improve this answer
    
He's looking for 3 bytes... –  Chris S Feb 21 '13 at 16:56
1  
3 plus exact pattern –  ADM Feb 21 '13 at 17:05
    
This is excellent. –  Danijel J Jan 31 '14 at 3:28
    
Thats excellent. More then OP asked! –  TheHippo Feb 5 '14 at 12:01
1  
Great! There are also the char class names ([:graph|print|etc:]) available for the tr func, and it could be further simplified by replacing the fold -w 3 | head -n 1 by a head -c 3 if the new line char is not needed at the end. –  Zimmi Dec 14 '14 at 22:39

Try this: dd if=/dev/urandom bs=1 count=3

If you want to put the result in $variable:

variable=`dd if=/dev/urandom bs=1 count=3`

Do note that it'll probably not be printable.

share|improve this answer

Here's one that creates base64 strings, note that even though they are limited to base64 strings, the padding is removed from them, so you can't decode them, you probably won't need it anyway.

 cat /dev/urandom | base64 | head -c 5

Replace 5 with the number of chars you'd like.

If you however need to decode them for some reason, move base64 to the end of the pipe. It will then collect 5 chars from urandom and base64 encode it with the right padding, but the final string may be longer than what you wanted due to padding.

cat /dev/urandom | head -c 5 | base64
share|improve this answer
    
This is not exactly what the question was asking for. Your answer might still be useful, but the question doesn't have enough context to say for sure. –  kasperd Jan 4 at 10:23
    
@kasperd The question asks (in the title) how to read N random characters from /dev/urandom but in the body it asks for reading bytes. I agree there is an inconsistency. I came to this question from Google for reading characters and I think that it may help the future visitors as an option. –  Umur Kontacı Jan 4 at 14:31

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.