The “sleep” command of Bash is used to stop the execution of the script for a certain period. This command is useful to do any scheduled task or to wait for a certain period before executing a particular script. The command stops the execution of the script for some specific times. The uses of this command in Bash for various purposes are shown in this tutorial.
Syntax:
sleep number[suffix]…
The value of the number can be a positive integer or a floating-point number. The default value of the suffix is “s” which indicates seconds. The other values of the suffix can be “m” for minutes, “h” for hours, and “d” for days.
Different Uses of the “Sleep” Command
Example 1: Using the “Sleep” Command with the Integer Value
Create a Bash file with the following script that prints the number from 1 to 5 with 2 seconds intervals using the “sleep” command. An infinite for loop is used to print the $counter value after 2 seconds until the $counter value is equal to 5. The “sleep” command is used with the value 2 to set the 2 seconds interval. No suffix is used here.
#Set the counter value
counter=1
#Infinite loop
for((;;))
do
#Check the counter value
if [ $counter -gt 5 ]; then
break
fi
#Print the counter value
echo $counter
#wait for 2 seconds
sleep 2
#Increment the counter value
((counter++))
done
The following output appears after executing the script:
Example 2: Using the “Sleep” Command with a Floating-Point Number
Create a Bash file with the following script that prints the “Digital Clock” text and the current system time that updates every second using the “sleep” command. An infinite for loop is used to update and print the current time every second. The “s” is used as the suffix of the “sleep” command. Each character of the text is printed after 0.5 seconds interval. The user has to press “Ctrl+C” to terminate the script.
#Display the line of text using the 'sleep' command
echo -n 'D'; sleep 0.5s
echo -n 'i'; sleep 0.5s
echo -n 'g'; sleep 0.5s
echo -n 'i'; sleep 0.5s
echo -n 't'; sleep 0.5s
echo -n 'a'; sleep 0.5s
echo -n 'l'; sleep 0.5s
echo -n ' '; sleep 0.5s
echo -n 'C'; sleep 0.5s
echo -n 'l'; sleep 0.5s
echo -n 'o'; sleep 0.5s
echo -n 'c'; sleep 0.5s
echo -n 'k'; echo
echo "Clock will display soon."
echo "Press Ctrl+C to close the program."
sleep 3s; clear
while true
do
#Display the time
date +"%H:%M:%S"
#Wait for 1 second
sleep 1s
#Clear the screen every second
clear
done
The following output appears after executing the script. The script waits for 5 seconds after displaying the following message:
The following output appears after waiting for 5 seconds and the value of the time is updated in every second that is displayed like a digital clock. Press Ctrl+C to terminate the script:
Example 3: Using the “Sleep” Command to Create an Alarm
You have to install a media player before testing the script of this example. Run the following commands to update the system and install the vlc player into the system:
$ sudo apt install vlc
Create a Bash file with the following script that takes the time of the alarm in hours, minutes, and seconds. The vlc player opens the media file automatically after passing the hours, minutes, and seconds that are taken as input. Select an existing media file that will be played in the media player. In this script, the “alarm.mp3” file is played using the “nvlc” command after the interval passed by the “sleep” command.
echo "Playing alarm..."
#Set the alarm time
echo -n "Enter the alarm time (_h _m _s): "
read alarm_time
#Start the VLC Player when the alarm time reaches
sleep $alarm_time && nvlc /mnt/c/temp/alarm.mp3
The following output appears after executing the script. Here, the interval is set to 10 seconds. The vlc player is opened with the media file after 10 seconds:
The following output appears when the media player starts playing the media file. The output shows the location of the file, the state of the media player, the position of the media file, and the volume of the sound:
Conclusion
The “sleep” command is a very useful command of Bash that is used for various purposes. Some uses of this command are shown in this tutorial using multiple examples such as displaying the animated text and digital clock, creating the alarm clock, etc. The method of using this command in the Bash script will be cleared for the Bash users after reading this tutorial.