Random number from a range in a Bash Script

Jason Gooner picture Jason Gooner · Mar 31, 2010 · Viewed 186k times · Source

I need to generate a random port number between 2000-65000 from a shell script. The problem is $RANDOM is a 15-bit number, so I'm stuck!

PORT=$(($RANDOM%63000+2001)) would work nicely if it wasn't for the size limitation.

Does anyone have an example of how I can do this, maybe by extracting something from /dev/urandom and getting it within a range?

Answer

leedm777 picture leedm777 · Mar 31, 2010
shuf -i 2000-65000 -n 1

Enjoy!

Edit: The range is inclusive.