1236

I have only found how to wait for user input. However, I only want to pause so that my while true doesn't crash my computer.

I tried pause(1), but it says -bash: syntax error near unexpected token '1'. How can it be done?

11 Answers 11

2137

Use the sleep command.

Example:

sleep .5 # Waits 0.5 second.
sleep 5  # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.

One can also employ decimals when specifying a time unit; e.g. sleep 1.5s

Sign up to request clarification or add additional context in comments.

5 Comments

It seems on Mac OS X, the s, m, h, nor d have any impact. You must specify the time in seconds.
I think the naming is because we have thread.sleep function in many programming languages @Geremia
More recently the suffix gives an error on Mac. "usage: sleep seconds"
If you get an "invalid time interval" you might want to check for your end of line setting, if you created your script with a Windows tool your end of line is "CR LF", you need to change it for Linux which is "LF" only. (Actually the error should be self explicit: invalid time interval '1\r' here you can see the extra \r from the \r\n)
ℹ️ Avoid an extra process by doing enable sleep then builtin sleep [time]
251

For those looking for the Bash equivalent of Windows Powershell/CMD's pause command.

In Bash use read with option -p specifying a prompt like:

read -p "Press enter to continue"

5 Comments

Actually, this answered my question, even if it doesn't answer the OP.
this has nothing to do with this post.
@murtadhaalsabbagh, But has very much to do with Google indexing :) Helped me too, btw.
read -p "Press enter to continue" -t 1 should pause for 1 sec
@JesseChisholm I guess people are looking for the bash equivalent of the powershell/cmd PAUSE
95

In Python (question was originally tagged Python) you need to import the time module

import time
time.sleep(1)

or

from time import sleep
sleep(1)

For shell script is is just

sleep 1

Which executes the sleep command. eg. /bin/sleep

4 Comments

yes, this says it too, it's just the other one was first, and it has a nice example :) but +1!
So while not the right way to do it, you can combine the python answer with Bash by using python -c "import time; time.sleep(1)" instead of sleep 1 :)
@BerryM. - Takes about 1.2 seconds when I try it. Python doesn't start up instantly - you need to account for that. Make it python -c "import time; time.sleep(0.8)" instead. But then we need to factor in how long python startup actually takes. You need to run this: date +%N; python -c "import time; time.sleep(0)"; date +%N to determine how many nanoseconds python is taking to start. But that also includes some overhead from running date. Run this date +%N; date +%N to find that overhead. Python's overhead on my machine was actually closer to 0.14 seconds. So I want time.sleep(0.86).
True, though you'll never get exactly 1000ms, not even like that.
78

Run multiple sleeps and commands

sleep 5 && cd /var/www/html && git pull && sleep 3 && cd ..

This will wait for 5 seconds before executing the first script, then will sleep again for 3 seconds before it changes directory again.

4 Comments

+1 ... cuz if you use one ampersand after the sleep it sends sleep off to its own thread then immediately starts the next action (i.e. the sleep doesn't delay the next action)
Why && and not ;?
@vrnvorona && will wait for the first command to finish.
@RobotBoy i think most importantly, if first command fails, && won't proceed, but ; will. ; will wait too, but won't check success.
47

On macOS, the sleep command can be used to sleep for a number of seconds. However, unlike other varieties of Unix, it does not take minutes/etc., only seconds. So for two minutes:

sleep 120

Comments

46

I realize that I'm a bit late with this, but you can also call sleep and pass the disired time in. For example, If I wanted to wait for 3 seconds I can do:

/bin/sleep 3

4 seconds would look like this:

/bin/sleep 4

1 Comment

I needed this not the python one. Thanks.
24

Within the script you can add the following in between the actions you would like the pause. This will pause the routine for 5 seconds.

read -p "Pause Time 5 seconds" -t 5
read -p "Continuing in 5 Seconds...." -t 5
echo "Continuing ...."

3 Comments

This wont actually work, will it? I think that -t 5 will abort the script after 5 seconds, not continue, at least according to this man page for read
@BradParks Yes it will work Just in case: root@Joses-iPad:~ # cat readtest.bash #!/bin/bash echo "Here" read -p "Pause Time 5 seconds" -t 5 read -p "Continuing in 5 Seconds...." -t 5 echo "Continuing ...." echo "There" root@Joses-iPad:~ # cat re./st.bash Here Pause Time 5 secondsContinuing in 5 Seconds....Continuing .... There
Hmmm.... I think you're right! I thought I tried this, but I just tried it again and it works as advertised. Thanks!
16
read -r -p "Wait 5 seconds or press any key to continue immediately" -t 5 -n 1 -s

To continue when you press any one button

for more info check read manpage ref 1, ref 2

2 Comments

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
This answer is a code-only comment. It does not attempt to explain the code given. I feel the poster misinterpreted the question as "How do I wait for user input while simultaneously having a timeout on that user input"
4

I think you can use software flow control.

Ctrl+s Freeze the terminal
Ctrl+q Unfreeze

https://en.wikipedia.org/wiki/Software_flow_control

$ while :; do sleep 0.2; echo hello; done
hello
hello
hello
hello

Use Ctrl+s to pause and Ctrl+q to resume the infinite loop.

Comments

3

You can make it wait using $RANDOM, a default random number generator. In the below I am using 240 seconds. Hope that helps @

> WAIT_FOR_SECONDS=`/usr/bin/expr $RANDOM % 240` /bin/sleep
> $WAIT_FOR_SECONDS

1 Comment

Is $WAIT_FOR_SECONDS really supposed to be on the next line? It seems like it was intended to be the argument to the /bin/sleep command. Why not use /bin/sleep $((RANDOM % 240))?
3

use trap to pause and check command line (in color using tput) before running it

trap 'tput setaf 1;tput bold;echo $BASH_COMMAND;read;tput init' DEBUG

press any key to continue

use with set -x to debug command line

Comments