Dave's Brain

Browse - Programming Tips - How can I have a random delay in a bash script?

Date: 2016jun15
Language: bash

Q.  How can I have a random delay in a bash script?

A.  Use $RANDOM and sleep like this:

#!/bin/sh # Make a random number less than 10 delay=$((RANDOM % 10)) # Display it echo "delay=$delay" # Sleep for it sleep $delay
Or simply:
sleep $((RANDOM % 10))
Note that case matters, $random doesn't work.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.