Random

tl;dr Use $RANDOM to get random numbers in bash scripts.

Quick tip: yesterday I learned that bash provides $RANDOM to give you pseudo-random variables on demand.

echo $RANDOM $RANDOM
26638 7429

Though it looks like a variable, apparently it’s a function! From the linked page:

$RANDOM is an internal Bash function (not a constant) that returns a pseudorandom integer in the range 0 – 32767. It should not be used to generate an encryption key.

So hey, don’t use it for crypto, but for quick hacks? (I used it to generate a series of temporary file names.) Very nice! 👌 Sure beats the incantations for pulling stuff out of /dev/urandom, amirite?

Questions? Comments? Contact me!

Tools Used

bash
5.0.11(1)-release