read /dev/urandom 3
The above is not working..How can I read random bytes from /dev/urandom in bash?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this communityread /dev/urandom 3
The above is not working..How can I read random bytes from /dev/urandom in bash?
random="$(dd if=/dev/urandom bs=3 count=1)"
It treats the output of a command like a variable.,you actually mean as string,right?
$() 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.
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 as done above, as opposed to *-_).
And to explain what gets done due to the above set of commands:
head -c 500 /dev/urandom: Obtain the first 500 characters (bytes) from /dev/urandom. tr -dc 'a-zA-Z0-9~!@#$%^&*_-': Remove all but the characters specified in 'a-zA-Z0-9~!@#$%^&*_-' from the output of the first command.fold -w 3: Format the output of the second command such that it has 3 characters per line. head -n 1: Display the first line of the result of the third command on stdout.fold -w 3 | head -n 1 by a head -c 3 if the new line char is not needed at the end.
Illegal byte sequence error. To fix that, use the "C" encoding; change the command to: head -c 500 /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9~!@#$%^&*_-' | fold -w 3 | head -n 1
cat /dev/urandom | tr -dc 'a-zA-Z0-9~!@#$%^&*_-' | fold -w 3 | head -n 1
Please check man od.
You can use, for example
od -vAn -N4 -tu4 < /dev/urandom
to generate unsigned decimal 4 bytes random numbers.
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
cat will read far more than a given amount N chars, and can deplete the entropy. Not useful answer.
cat since both base64 and head can take a filename as an argument, in this case it works and shouldn't deplete the entropy. See stackoverflow.com/questions/10031344/…
$ head -c 5 /dev/urandom | base64 produces exactly the same output.
Oct 2 '17 at 23:42
The easiest solution would be as simple as:
$ head -cN /dev/urandom
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.