0

I am writing a bash script which simply needs to sleep for less than a second. However 'sleep' only accepts seconds as input.

Is there any command to sleep less than 1000 ms?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
TheUnseen
  • 314
  • 2
  • 6

2 Answers2

4

sleep from GNU Core Utilities does accept decimal numbers. From sleep(1):

Pause for NUMBER seconds. SUFFIX may be s for seconds (the default), m for minutes, h for hours or d for days. NUMBER need not be an integer. Given two or more arguments, pause for the amount of time specified by the sum of their values.

I also tested BusyBox version of sleep and confirmed that it also supports decimal numbers. This should clear any issues with even Alpine Linux.

iBug
  • 35,554
  • 7
  • 89
  • 134
1

The easiest workaround I could find is using bash's read builtin, which accepts milliseconds:

read -t 0.5

or with non bash scripts, for example fish:

bash -c 'read -t 0.5'
TheUnseen
  • 314
  • 2
  • 6
  • Beware that 'read -t' times out after the specified time and returns an error value (142). It is actually a timeout function for reading user input, hence the value > 0 – TheUnseen Nov 28 '20 at 10:07