Bash sleep until time

How to Bash sleep until time or delay a specific amount of time. How do I pause for 5 seconds in a bash shell script on a Linux? Bash script the essential for DevOps Roles.

In Linux, sleep command to add delay for a specified amount of time.
The syntax sleep command

sleep NUMBER[SUFFIX]

Where SUFFIX:

  • s for seconds (the default)
  • m for minutes.
  • h for hours.
  • d for days.

Bash sleep until time

Examples

To sleep for 5 seconds

sleep 5

To sleep for 5 minutes

sleep 5m

How do I pause my bash shell script until time for 5 second

In DOS, To pause command execution until the user pressed a key. In Linux, Use read command with the -p option

read -rsp

Explanation

-r specifies the raw mode, which doesn’t allow combined characters like “\” or “^”.
-s specifies the silent mode
-p $’prompt’ specifies the prompt
-n 1 specifies that it only needs a single character.
-t 5 specifies a timeout of 5 seconds

Bash shell pause function

#!/bin/bash
# init
function pause(){
read -sp "$*"
}

pause 'Press [Enter] key to continue...'

The screen output terminal:

Bash sleep until time

Conclusion

Thought the article, you can use Bash sleep until the time as above. I hope will this your helpful. More details refer to Bash script.

Press any key or wait 5 seconds to continue...\n' -n 1 -t 5;

Explanation

-r specifies the raw mode, which doesn’t allow combined characters like “\” or “^”.
-s specifies the silent mode
-p $’prompt’ specifies the prompt
-n 1 specifies that it only needs a single character.
-t 5 specifies a timeout of 5 seconds

Bash shell pause function

The screen output terminal:

Bash sleep until time

Conclusion

Thought the article, you can use Bash sleep until the time as above. I hope will this your helpful. More details refer to Bash script.

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published.

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.