Bash sleep in milliseconds

I need a timer that will work with milliseconds. I tried using the sleep 0.1 command in the script. I see an error message:

syntax error: invalid arithmetic operator (error token is ".1")

When I run sleep 0.1 in the terminal, it works fine.

Please help me!

EDIT: Sorry, I made a mistake:

 function timer { while [[ 0 -ne $SECS ]]; do echo "$SECS.." sleep 0.1 SECS=$[$SECS-0.1] done } 

Line sleep 0.1 was 5th, and SECS=$[$SECS-0.1] was 6th. I just crippled the lines. The problem was in the 6th line, because bash cannot work with floating point numbers. I changed my function as below:

 MS=1000 function timer { while [[ 0 -ne $MS ]]; do echo "$SECS.." sleep 0.1 MS=$[$MS-100] done } 
+9
bash sleep timer
source share
3 answers

Make sure you run your script in Bash, not /bin/sh . For example:

 #!/usr/bin/env bash sleep 0.1 

In other words, try explicitly specifying a shell. Then do either: ./foo.sh or bash foo.sh

In case sleep is an alias or function, try replacing sleep \sleep .

+19
source share

Some options:

 read -p "Pause Time .5 seconds" -t 0.5 

or

 read -p "Continuing in 0.5 Seconds...." -t 0.5 echo "Continuing ...." 
+3
source share

Bash complained about decimal values,

read: 0.5: invalid timeout specification

I came up with this solution that works great.

 sleep_fraction() { /usr/bin/perl -e "select(undef, undef, undef, $1)" } sleep_fraction 0.01428 
0
source share

All Articles