Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Bash Tips & Tricks»Adding Time Delays in Shell Scripts

    Adding Time Delays in Shell Scripts

    By RahulJune 14, 20233 Mins Read

    Shell scripting is a robust tool that can automate repetitive tasks, control system processes, and perform complex operations. An important part of mastering shell scripting is understanding how to add delays or pauses in your scripts. This can be handy, for instance, when you want to space out commands or ensure certain processes have finished before moving on. Here, we provide a step-by-step guide to adding delays in your shell scripts using the ‘sleep’ command.

    What is a Shell Script?

    Before we delve into the specifics of adding delays, let’s quickly cover what a shell script is. A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

    The Basics of Adding Delays

    The ‘sleep’ command is used in shell scripts to pause the execution of the next command for a specified amount of time. The basic syntax of the ‘sleep’ command is straightforward:

    1
    sleep NUMBER[SUFFIX]

    In this case, NUMBER specifies the length of the delay, and SUFFIX may be ‘s’ for seconds (the default), ‘m’ for minutes, ‘h’ for hours, or ‘d’ for days. For example, ‘sleep 5’ would pause for five seconds, while ‘sleep 2m’ would pause for two minutes.

    Step-by-Step Guide to Adding Delays

    Now that we understand the basic syntax of the ‘sleep’ command, let’s explore how to use it within a shell script.

    1. Open your text editor: Shell scripts are written in plain text, so you can use any text editor to write them. This includes vi, nano, or emacs on Unix-based systems, or programs like Notepad++ on Windows.
    2. Start your script: Every shell script begins with a shebang (#!) followed by the path to the shell. For most scripts, you’ll use /bin/bash, like so:

      1
      #!/bin/bash

    3. Write your commands: After defining the shell, you can begin scripting. For instance, you might want to print text, then pause, then print more text:

      1
      2
      3
      4
      #!/bin/bash
      echo "Before sleep"
      sleep 5
      echo "After sleep"

    4. Save your script: Save the script with a .sh extension, such as delayScript.sh. This identifies it as a shell script.
    5. Make your script executable: Before you can run your script, you’ll need to make it executable. You can do this with the chmod command:
      chmod +x delayScript.sh 
      
    6. Run your script: Finally, you can run your script. If you’re in the same directory as the script, you use the ./ prefix:
      ./delayScript.sh 
      

      The script will print “Before sleep”, pause for five seconds, then print “After sleep”.

    Further Examples

    You can add delays between any commands in your shell scripts. The following script, for example, would print a number, wait for a second, then print the next number:

    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash
     
    for i in {1..10}
    do
       echo $i
       sleep 1
    done

    You can also use a variable to set your sleep time:

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    DELAY=3
    echo "I will wait for $DELAY seconds"
    sleep $DELAY
    echo "I'm back!"

    Conclusion

    The ability to add delays to shell scripts is a powerful tool. It allows for more precise control over the execution of your scripts and can be critical in a wide range of tasks. By using the ‘sleep’ command and understanding its syntax, you can elevate your scripting to new levels. Happy scripting!

    bash Sleep
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Assignment Operators in Bash

    An Introduction to Bash Variables

    An Introduction to Bash Variables

    Bash LOCAL and GLOBAL Variables

    Bash LOCAL and GLOBAL Variables

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Execute Linux Commands in Python
    • Creating MySQL User with GRANT OPTION
    • Where to find crontab (cron) logs in Ubuntu & Debian
    • Backing Up Docker Volumes and Upload to S3
    • Difference Between Full Virtualization vs Paravirtualization
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.