Infinite Loop

Learn how to implement infinite loop and where it can be used.

Infinite loop

The while statement fits well when we need to implement an infinite loop. This kind of loop continues as long as the program is running.

Infinite loops are often used in system software. They run the whole time while a computer stays powered on. An example is the microcontroller firmware that checks some sensors cyclically. It happens in an infinite loop. Also, such loops are used in computer games, antiviruses, monitors of computer resources, etc.

The while loop becomes infinite if its condition always stays true. The easiest way to make such a condition is to call the built-in Bash function true. Here is an example for doing so:

while true
do
  sleep 1
done

Run the commands discussed in this lesson in the terminal below.

Get hands-on with 1400+ tech skills courses.