read /dev/urandom 3

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

link|improve this question

45% accept rate
feedback

2 Answers

up vote 3 down vote accepted
random="$(dd if=/dev/urandom bs=3 count=1)"
link|improve this answer
@Flimzy,how does $() work? – linux Jun 23 '11 at 6:07
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
1  
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
show 2 more comments
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.