Normally, a Bash script will execute each line of code the moment it reaches it, then immediately move on to the next. But it is also possible to add pauses to a Bash script in order to delay it or allow the user time to respond to a prompt, etc.
In this tutorial, you will learn how to add pauses to a Bash script on a Linux system. Check out our examples below to see how pauses can be facilitated by the
sleep command, and how this functionality has practical use in some scripts.
In this tutorial you will learn:
- How to pause Bash script with
- How to pause Bash script with
- How to make a prompt that has a timer in Bash scripting
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Bash shell (installed by default)
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Bash script examples: Pause script before proceeding
- Let’s start with a basic example of the
sleepcommand. This is easy to use, and allows us to pause our Bash script for any amount of time in seconds, minutes, hours, or even days.
#!/bin/bash echo "Script will proceed in 5 seconds..." sleep 5s echo "Thanks for waiting."
Obviously this script does not have much practicality, but you can see how the
sleepcommand works. You can also use decimals with
sleepand other units of time as mentioned above. Note you do not need to include the
sfor seconds, it is optional.
sleep 10 # pauses for 10 seconds sleep 5.5 # pauses for 5.5 seconds sleep 10m # pauses for 10 minutes sleep 3h # pauses for 3 hours sleep 3.5h # pauses for 3 hours, 30 minutes sleep 2d # pauses for 2 days
- The example above shows us how the
sleepcommand works, but how would it be useful in a real Bash script? Pausing a script proves very useful in loops, specifically. Loops tend to execute very quickly at times, and can overwhelm your system’s resources if you do not employ a
sleepcommand or similar to force the loop to take a break.
#!/bin/bash var=0 while [ $var -lt 4 ] do ssh email@example.com sleep 1m ((var++)) done
The script above will continuously try to establish an SSH connection with a host, up to five times. This is a nice way to try and get a connection to a computer which is in the process of coming online, and you do not want to keep entering the SSH command yourself. The
sleepcommand in our script prevents the
whileloop from spamming the
sshcommand, by forcing it to pause for one minute. This is just one example of how pausing your script with the
sleepcommand can be very handy.
- We can also use the
readcommand to pause our Bash script. Use the
-tcommand and the number of seconds to pause the script. We are also including the
-poption and some informative text in this example, but it is not strictly necessary.
#!/bin/bash read -p "Pausing for 5 seconds" -t 5 echo "Thanks for waiting."
This method is nice because, to skip the timer, you can simply press
Enteron your keyboard to force the timer to expire and have the script proceed. Returning to our SSH script in the previous example, imagine if we had used the
readcommand instead of
sleep, so that we could force a new SSH attempt if we got impatient for the
whileloop to be triggered again.
- Since the
readcommand is normally used to read input from the command line, the
-toption allows us to make our user prompt expire after a certain time. Let’s look at a practical example.
#!/bin/bash read -p "Do you want to proceed? (yes/no) " -t 10 yn if [ -z "$yn" ] then echo -e "\nerror: no response detected" exit 1 fi case $yn in yes ) echo ok, we will proceed;; no ) echo exiting...; exit;; * ) echo invalid response; exit 1;; esac echo doing stuff...
The script above is a simple yes or no prompt. These are very common throughout Linux and Bash scripts, which usually ask a user if they would like to proceed with something. In the script above, our
-t 10option in the
readcommand will make the script proceed after 10 seconds, unless the user enters a response before then. Our
ifstatement is triggered if an empty response is detected, and will issue an error and exit. If a response is detected, then the
casestatement is triggered.
In this tutorial, you saw how to pause a Bash script before proceeding. We tried to stay away from silly examples in the tutorial, and only show practical scripts that have a useful application in the real world. The goal was to allow you to see how a
sleep pause and a
read pause can both be useful in different situations.