1051

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

1821

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

4
  • 89
    It seems on Mac OS X, the s, m, h, nor d have any impact. You must specify the time in seconds. Aug 19, 2017 at 15:31
  • 7
    I think the naming is because we have thread.sleep function in many programming languages @Geremia
    – Amin_mmz
    Jan 31, 2018 at 13:14
  • More recently the suffix gives an error on Mac. "usage: sleep seconds"
    – nzifnab
    Jul 1, 2022 at 18:33
  • 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)
    – Romano
    Sep 1, 2022 at 15:39
191

And what about:

read -p "Press enter to continue"
5
  • 81
    Actually, this answered my question, even if it doesn't answer the OP. Mar 18, 2017 at 1:56
  • 18
    this has nothing to do with this post. Feb 27, 2018 at 7:47
  • 17
    @murtadhaalsabbagh, But has very much to do with Google indexing :) Helped me too, btw. Jun 27, 2019 at 20:37
  • 7
    read -p "Press enter to continue" -t 1 should pause for 1 sec
    – SzB
    Mar 4, 2020 at 15:11
  • 6
    @JesseChisholm I guess people are looking for the bash equivalent of the powershell/cmd PAUSE
    – rovyko
    Mar 15, 2020 at 3:35
91

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
  • 6
    yes, this says it too, it's just the other one was first, and it has a nice example :) but +1!
    – user3268208
    Feb 7, 2014 at 6:05
  • 4
    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 :)
    – Berry M.
    Jul 10, 2017 at 7:11
  • 3
    @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). Dec 12, 2017 at 21:51
  • True, though you'll never get exactly 1000ms, not even like that.
    – Berry M.
    Dec 13, 2017 at 5:24
65

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
  • 5
    +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)
    – Jay Marm
    Sep 28, 2018 at 0:38
  • Why && and not ;?
    –  vrnvorona
    Jun 10, 2021 at 17:17
  • @vrnvorona && will wait for the first command to finish.
    – Robot Boy
    Aug 6, 2021 at 7:11
  • 3
    @RobotBoy i think most importantly, if first command fails, && won't proceed, but ; will. ; will wait too, but won't check success.
    –  vrnvorona
    Aug 7, 2021 at 8:03
45

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
  • I needed this not the python one. Thanks.
    – Amit Ray
    Sep 18, 2022 at 10:26
43

On Mac OSX, sleep does not take minutes/etc, only seconds. So for two minutes,

sleep 120
23

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
  • 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
    – Brad Parks
    Nov 18, 2019 at 16:48
  • @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 Nov 29, 2019 at 4:58
  • Hmmm.... I think you're right! I thought I tried this, but I just tried it again and it works as advertised. Thanks!
    – Brad Parks
    Nov 29, 2019 at 13:24
14
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
  • 8
    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.
    – peacetype
    Feb 25, 2018 at 5:33
  • 2
    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"
    – Zimano
    Aug 22, 2018 at 11:43
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
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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy