1

I wanted to generate a random decimal that was rounded to the 10ths place between .1 and 1.

I did this with the command

`echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`

Now if you set it as a variable, say

var=`echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`

and have your script echo var like so,

#!/bin/bash
var=`echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`
echo $var
echo $var
echo $var

It repeats the same decimal, say .4, but if you

#!/bin/bash
echo `echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`
echo `echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`
echo `echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`

It will give you three random numbers using the same command as

$var=`echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`

Why does Bash not generate three new numbers if given the same command but as a variable?

2
  • Re your last example -- var=foo, not $var=foo. Commented Apr 21, 2015 at 1:13
  • Actually $var=/=foo because it was defined at the top of the script. Commented Apr 21, 2015 at 1:16

2 Answers 2

4

The following command sets a value for var:

var=`echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`

Once the value is set, it does not change. Thus, however many times you view it, it will be the same:

echo $var
echo $var
echo $var

The only way to get a new value is for bash to evaluate RANDOM again.

Using a function instead

You might prefer a function that would return a different random variable with each invocation:

$ var() { echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l; }
$ var
.3
$ var
.4
$ var
1.0
$ var
.4
Sign up to request clarification or add additional context in comments.

7 Comments

I see. I knew bash stored variables, but wasn't sure why it wouldn't execute the command on each new line! Thanks for the one-liner function idea. :)
Is there any way of adding functions? I have #!/bin/bash var() { echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l; } add=$((var+var+var+var+ var)) echo $add but I'm getting an operand error. Same thing with quotations around var.
@CharlesDuffy, When I make my scripts I set it to #!/bin/bash and run with ./scriptname. My default interpreter is also bash not sh.
Oh. It's floating-point. Bash doesn't support floating-point math natively.
See BashFAQ #22: mywiki.wooledge.org/BashFAQ/022; this is why you're using bc rather than $(( )) alone in the first place.
|
2

It is because

var=`echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l`

executes the echo command and stores its output into a variable. The command itself is only evaluated once by the bash interpreter.

If you want a compact way to generate a random number, I suggest using a function:

#!/bin/bash
myRandom(){
    echo "scale=1; $(( ( RANDOM % 10 ) + 1 ))/10" | bc -l
}

echo $(myRandom)
echo $(myRandom)
echo $(myRandom)

3 Comments

Using backticks? Not echo $(myRandom)?
@cdarke Both are possible. AFAIK, the $()-syntax was adopted later for readability (from Korn shell).
If by later you mean about 20 years ago, then yes, but the backtick form is considered obsolete in bash and if you use ksh -n then it is flagged as deprecated. See wiki.bash-hackers.org/syntax/expansion/cmdsubst

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.