Back to Homepage
Wednesday 7 August 2024
17

How to sleep in Bash

Steps to Use Sleep in Bash

Need to pause execution in Bash? Follow these easy steps:

1. Basic Sleep Command:

  • Use the sleep command followed by the number of seconds to pause:
#!/bin/bash
     echo "Start"
     sleep 5
     echo "End after 5 seconds"

2. Sleep for Minutes:

  • Use m to specify minutes:
#!/bin/bash
     echo "Start"
     sleep 2m
     echo "End after 2 minutes"

3. Sleep for Hours:

  • Use h to specify hours:
#!/bin/bash
     echo "Start"
     sleep 1h
     echo "End after 1 hour"

4. Sleep for Days:

  • Use d to specify days:
#!/bin/bash
     echo "Start"
     sleep 1d
     echo "End after 1 day"

5. Sleep with Fractions of a Second:

  • Use fractional values for sub-second delays:
#!/bin/bash
     echo "Start"
     sleep 0.5
     echo "End after 0.5 seconds"
Share:
Created by:
Author photo

Jorge García

Fullstack developer