Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash sleep Usage: Pause for a specified duration
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash sleep Usage: Pause for a specified duration

Overview

The sleep command in Bash is used to pause the execution of a script or command for a specified amount of time. This can be useful in various scenarios, such as waiting for a process to complete or introducing delays in automated scripts.

Usage

The basic syntax of the sleep command is as follows:

sleep [options] [duration]

Where [duration] can be specified in seconds, or with a suffix to indicate minutes (m), hours (h), or days (d).

Common Options

  • -h, --help: Display help information about the command.
  • -V, --version: Show the version of the sleep command.

Common Examples

  1. Pause for 5 seconds:
    sleep 5
    
  2. Pause for 2 minutes:
    sleep 2m
    
  3. Pause for 1 hour:
    sleep 1h
    
  4. Pause for 3 days:
    sleep 3d
    
  5. Using sleep in a loop:
    for i in {1..5}; do
        echo "Iteration $i"
        sleep 1
    done
    

Tips

  • Use sleep to create delays between commands in scripts, especially when waiting for resources to become available.
  • Combine sleep with other commands in a script to manage timing effectively, such as in automated testing or deployment scripts.
  • Be mindful of the duration specified; using excessively long sleep times can lead to unresponsive scripts.