To generate random numbers in Bash, you can use the $RANDOM
variable, which returns a random integer between 0 and 32767 (inclusive).
For example, to generate a random number and print it to standard output, you can use the following command:
$ echo $RANDOM
This command will print a random integer between 0 and 32767 to standard output.
If you want to generate a random number within a specific range, you can use the $RANDOM
variable in combination with the $(( ... ))
arithmetic expression syntax, like this:
$ echo $(( $RANDOM % 10 + 1 ))
This command will generate a random integer between 1 and 10 (inclusive), by using the $RANDOM
variable to generate a random integer between 0 and 32767, and then using the $(( ... ))
syntax to calculate the remainder of the random integer divided by 10, and add 1 to the result.
Overall, the $RANDOM
variable is a simple and effective way to generate random numbers in Bash. By using this variable, you can easily and quickly generate random integers within a specific range, allowing you to use random numbers in your scripts and on the command line.