Re:Random Numbers in Bash
by TRiG_Ireland on Monday March 27, 2023 @12:46PM
(#35328962)
I use this to create passwords when I need to manually:
function password_generate { head -c ${1-12} /dev/urandom | base64; }Sometimes, extreme portability makes us do desperate things.
# uname -a
HP-UX prdcrm B.10.20 A 9000/800 862741461 two-user license
# ls -l /dev/urandom
/dev/urandom not found
# openssl rand -base64 8
v51U1RvSD2Q=
I could run this on VAX VMS if you want.
My favorite tool for this is "openssl rand" and I can find the manual page on rhel7 with "man rand."
Here are 8 bytes of randomness in base64:
When I've got dozens of users to create, this is how I am assigning their initial passwords.The shell's $RANDOM is somewhat less trustworthy than other sources here. It also does not appear to be POSIX, but comes from ksh88, where it is described as returning a random integer between 0 and 32767 each time it is referenced. If unset in a script, it cannot be used again for the life of the shell.