[Tips]Why command “sleep 1m” doesn’t work in mac

Problem

In my shell script, “sleep 1m” command doesn’t wait 1 minutes but 1 second.

sleep.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/sh
echo "start timer"
sleep 1m
echo "stop"
#!/bin/sh echo "start timer" sleep 1m echo "stop"
#!/bin/sh
echo "start timer"
sleep 1m
echo "stop"

Environment

macOS Catalina 10.15.5

The cause of this problem

In Mac OS X, sleep command doesn’t work with quantifiers and takes argument num just as num seconds.

Reference: OSX bash “sleep”, Mac OS X Man Pages

Solution

Use 60 times minutes.

sleep.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/sh
echo "start timer"
sleep 1*60
echo "stop"
#!/bin/sh echo "start timer" sleep 1*60 echo "stop"
#!/bin/sh
echo "start timer"
sleep 1*60
echo "stop"