Get Random Number in Bash
system variable $RANDOM can be used to generate a random number between 0 and 32767, in case you want to get a random number between 0 and 10, you may do the following
$ declare -i number=$RANDOM*10/32767
$ echo $number
declare options/typeset:
-i integer: following variable will be an integer
-r readonly: following variable will be readonly, cannot be reset.
-a array: following variable will be an array.
-f function
-x export: following variable will be exported.
$ sum=100+50
$ echo $sum
$ 100+50
$ declare -i sum=100+50
$ echo $sum
$ 150
Notes:
- when declare variable, there should be no space before/after “=”
- what is bash?
- defauly variable type is string
- if you declare variable as readonly by mistake, you need to logout and login again to undo the mistake