"Taking a LiveFire Labs' course is an excellent way to learn
Linux/Unix. The lessons are well thought out, the material is
explained thoroughly, and you get to perform exercises on a real
Linux/Unix box. It was money well spent."
Ray S.
Pembrook Pines, Florida
LiveFire Labs' UNIX and Linux Operating System Fundamentals
course was very enjoyable. Although I regularly used UNIX systems
for 16 years, I haven't done so since 2000. This course was a
great refresher. The exercises were fun and helped me gain a real
feel for working with UNIX/Linux OS. Thanks very much!"
Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada
Read more student testimonials...
Receive UNIX Tips, Tricks, and Shell Scripts by Email
LiveFire Labs' UNIX Tip,
Trick, or Shell Script of the Week
Useful Shell Script Variables - Part IV - RANDOM
RANDOM is a peculiar shell script variable, but useful
nonetheless. Peculiar because its value changes each time it is
referenced (yes, this is by design).
As you may have already guessed, RANDOM is a random number generator.
The number generated is an integer between 0 and 32767, and can come
in handy when writing shell scripts. To determine if a shell you're
using supports this variable, the following command can be used:
$ print $RANDOM $RANDOM
29302 8082
$
Two different numbers will be displayed if it’s supported, otherwise
you will see nothing.
Assigning a numeric value to RANDOM prior to referencing it will
initialize (seed) the sequence of random numbers:
$ RANDOM=10
$ print $RANDOM
4543
$ print $RANDOM
28214
$ print $RANDOM
11245
$
This same sequence of numbers can be repeated by initializing RANDOM
using the same seed:
$ RANDOM=10
$ print $RANDOM
4543
$ print $RANDOM
28214
$ print $RANDOM
11245
$
Just to get you thinking about potential uses for this handy variable,
consider the following...
When writing a shell script to automate the process of adding new user
accounts, it may be desirable to generate a unique initial password
for each account. Using the value provided by RANDOM for all or part
of the password would accomplish this. It would be wise to use the
PID of the process creating the account(s) to seed the generator:
<previous code>
RANDOM=$$
x=$RANDOM
<subsequent code>