HOWTO · Bash

How to Pause Bash Shell Script for X Seconds

Learn how to use read and sleep in Bash to pause a script

On this page

Bash scripts can pause to give someone time to read a message, wait for interactive input, or add a delay between commands. Use sleep for a whole-second delay and read when the script should wait for input. These pauses control when a script continues; a delay does not coordinate access to shared files.

Pause Bash Script for X Seconds

Use sleep with a whole number of seconds to delay the next command. The duration is required. The sleep manual describes the command; support for fractional values or duration suffixes depends on the sleep implementation installed on the system.

This complete Bash script prints a message, waits for one second, and then prints another message:

#!/usr/bin/env bash
printf '%s\n' 'The script is starting.'
sleep 1
printf '%s\n' 'One second wait requested.'

The requested delay is a target, not a guarantee of exact elapsed time: system scheduling can make the actual wait longer. To repeat an action after a whole-second pause, put the action and sleep in a loop. Keep the loop focused on that action so each repetition has a clear effect.

Pause Bash Script for a Variable Amount of Time

Store the whole-second duration in a variable and quote its expansion when passing it to sleep. This script uses five seconds by default; set N in the environment to choose another value:

#!/usr/bin/env bash
N=${N:-5}
sleep "$N"
printf 'Requested a %s-second wait.\n' "$N"

For example, N=8 ./your-script.sh sets the value for one invocation. The value must be a valid duration for the installed sleep command. Quoting prevents word splitting and pathname expansion, but does not make arbitrary input a valid duration, so validate values from users or other untrusted sources first.

For a bounded random whole-second delay in Bash, $RANDOM can be reduced to a range. The following script requests a delay from one through three seconds:

#!/usr/bin/env bash
delay=$((1 + RANDOM % 3))
printf 'Requested a %s-second wait.\n' "$delay"
sleep "$delay"

A random delay can vary when separate script runs begin, but it does not ensure that two runs will avoid writing to the same file at the same time. Use an appropriate locking or coordination mechanism when scripts share resources.

Pause Bash Script Until User Input Occurs

The Bash read command waits for a line on standard input. In an interactive terminal, the user normally completes the line by pressing Enter. Use -r to keep backslashes in the input unchanged, and -p to show a prompt:

#!/usr/bin/env bash
if read -r -p 'Press Enter to continue: ' reply; then
    printf '%s\n' 'Continuing.'
else
    printf '%s\n' 'No input line was available.'
fi

This pattern expects interactive input and can wait indefinitely for a line. If standard input reaches end-of-file or is redirected, read can return without receiving one; checking its status lets the script handle that case. The Bash read reference documents its options and behavior.

Combine Timed Pauses With User-Input Pauses

Use Bash read -t when input should end the wait early and a timeout should also let the script continue. The timeout is in seconds, and system scheduling can make the elapsed time longer. This example reports which event allowed the script to continue:

#!/usr/bin/env bash
if read -r -t 5 -p 'Continue now, or wait for the timeout: ' reply; then
    printf '%s\n' 'Input received; continuing.'
else
    printf '%s\n' 'No line was read before the wait ended; continuing.'
fi

To let a user choose a whole-second delay, read the value into a variable and pass the quoted value to sleep. This version checks for a short, numeric value before calling sleep and gives feedback when no suitable line is available:

#!/usr/bin/env bash
if read -r -p 'Enter a whole number of seconds (up to 3 digits): ' duration \
    && [[ $duration =~ ^[0-9]{1,3}$ ]]; then
    sleep "$duration"
    printf 'Requested a %s-second wait.\n' "$duration"
else
    printf '%s\n' 'Enter a whole number with at most 3 digits.'
fi

The value is checked before it reaches sleep, which prevents malformed text from being passed as a duration. Adjust the accepted range if your script needs different limits, and explain those limits to its users.

In short, use sleep for a timed whole-second delay and read for input. Bash read -t combines the two by ending the wait when a line arrives or the timeout expires.