1. Overview

Bash scripting enables us to automate tasks in Linux and Unix-based systems. For certain tasks, we may need to output text in a loop until a specific event occurs. For example, when a service is down, we could print log messages or send email notifications periodically until the service is restored.

In this tutorial, we’ll explore how to use Bash scripting for outputting text in a continuous loop until a specific keyboard key event occurs.

2. Sample Task

Let’s suppose we want to display the current time continuously as it updates every second. We also want the display to overwrite itself, in place, as time progresses, thus simulating a digital clock. Additionally, we should be able to quit the display at any time by pressing the q key on the keyboard.

This way, we poll for an event while running our main task.

Let’s explore how we can achieve this task.

3. Read User Input in a While Loop

To simulate a digital clock, we implement an infinite loop that also expects user input.

In particular, we can use a while loop with a condition that’s always true. Then, in each iteration, we can update the current time and read user input from the keyboard. If an input is provided and matches the q or Q character, we exit the while loop and the script ends.

Let’s implement these steps in a script named digital_clock.sh:

$ cat digital_clock.sh
#!/usr/bin/env bash

while true; do
    clear
    echo "Current Time: $(date +'%T')"
    read -t 0.1 -n 1 input
    if [ "${input,,}" == "q" ]; then
        echo
        break
    fi
done

In the script, we start with a shebang directive on the first line and then carry out several steps:

  1. create an infinite while loop using a condition that’s always true
  2. use the clear command to clear the screen, so we can update the time display in place
  3. print the current time using the date command
  4. use the read command to save any user input from the keyboard in a variable named input
  5. check if the value of input matches the q or Q character, and if so, print a newline and break from the while loop; otherwise, go back to step 3

Notably, the date +’%T’ command displays the current time. The command is executed in a subshell and its result is printed to the screen using echo.

When it comes to read, we use the -t option to set a timeout for user input with a value of 0.1 seconds. We also use the -n option to read only a specific number of characters, which we limit, in this case, to one character.

Finally, we use the ${input,,} expression for parameter expansion to obtain the lowercase value of the input variable. Then, we check if it matches the q character. This way, we account for both q and Q values that can be provided as user input.

4. Executing the Script

Next, we grant the script execute permissions using chmod:

$ chmod u+x digital_clock.sh

Finally, let’s run the script:

$ ./digital_clock.sh
Current Time: 08:56:18
q

Now, we should be able to see the current time as it updates every second. Moreover, we can exit the display upon pressing the q or Q key.

In general, by using the features of the read command, we manage to both see updates to the time, but also poll for user input for the potential termination event.

5. Conclusion

In this article, we explored how we can set up an infinite loop that outputs text until a keyboard key event occurs.

In particular, we simulated a digital clock using a while loop to continuously display the current time. Within the loop, we use the read command with a timeout to check for user input. If the input matches a particular key, such as q or Q, we break from the loop.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.