$RANDOM does not work inside a shell script

Hi folks

I'm coding on Ubuntu 9.04 standard shell.

I'm writing a script that needs to generate a random number at some point of its execution.

When I do

echo $RANDOM

as a command inside shell, I clearly get some randomly generated number

However when I do

i=`$RANDOM`
echo $i

or even

echo $RANDOM

as lines inside a shell script that I later run, I get nothing. Both options just give me a blank.

What am I doing wrong?

Try

i=$(echo $RANDOM)
echo $i

Guru.

No luck. Same output

Hi

RANDOM is not available in all shells. I know for sure it is there in ksh/sh,not there in csh/tcsh. Since its working on your prompt, try to run your script in the same shell as is your command line.

Guru.

RANDOM wasn't available in Bourne Shell.

Hey guys

So I've just moved to a new machine, and reinstalled Ubuntu 10.04 from scratch. I first tried

$ echo $RANDOM

in the shell. It works.
Then, when I tried the same command as a one line shell script called test.sh, and ran

$ sh test.sh

My output was a blank line.

So RANDOM is clearly available in this shell. Just that it doesn't work inside a script. Now what?

In Ubuntu sh is symlinked to dash.

d@DeCoBuntu:~$ ls  -ltrh /bin/sh
lrwxrwxrwx 1 root root 4 2010-05-26 02:41 /bin/sh -> dash
d@DeCoBuntu:~$ ^C

however, if you're just looking for some random numbers and letters, you can do this in pretty much any shell:

d@DeCoBuntu:~$ type rand
rand is a function
rand () 
{ 
    tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c$1;
    echo
}

Less efficient, but it'll get the job done.

At your normal prompt where $RANDOM is working:

echo $SHELL

Whatever that comes out try putting that in the shebang line in your script.

1 Like

try:

bash test.sh
or
ksh test.sh

Thanks. It fixes the immediate problem, but I'm curious as to why the RANDOM variable doesn't work inside the script, and I didn't fully understand the symlink explanation. Can you elaborate?

To paraphrase the earlier poster, when you type "sh" in your particular version of Linux you actually get "dash" because "sh" is a symbolic link to "dash".
In your case you probably needed to specify "bash".

In older unixes "sh" gave you the Bourne Shell but in most modern mainstream unixes "sh" gives you the Posix Shell. There is even more variation in Linux.

I'm not sure what the problem is. I'm using /bin/bash, and doing:

#!/bin/bash
i=$RANDOM
echo $i
i=`expr $i + 1`
echo $i

Works just fine..

---------- Post updated at 09:45 PM ---------- Previous update was at 09:45 PM ----------

I'm not sure what the problem is. I'm using /bin/bash, and doing:

#!/bin/bash
i=$RANDOM
echo $i
i=`expr $i + 1`
echo $i

Works just fine..

@methyl - Thanks

@eclipseagent - My code was the same as yours and wasn't working. I finally realized that I had been forgetting to do `chmod +x <filename>'! It works now. Thanks for helping me work out such an obvious error :stuck_out_tongue:

Actually.. i looked at yours and you had backticks ( ` ) in `$RANDOM`, which means you're trying to run the command.. which you wouldn't want to do.

That too :slight_smile:

Thanks again.