0
0
Bash Scriptingscripting~15 mins

Infinite loops in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Infinite Loops in Bash Scripting
📖 Scenario: You are learning how to create loops in Bash scripts. Loops help you repeat tasks automatically, like checking if a file exists or waiting for user input. Sometimes, you want a loop to run forever until you stop it manually. This is called an infinite loop.Imagine you want to create a simple script that keeps printing a message every second until you stop it. This is useful for monitoring or waiting for something to happen.
🎯 Goal: Build a Bash script that uses an infinite loop to print the message "Hello, world!" every second.
📋 What You'll Learn
Create an infinite loop using the correct Bash syntax
Use the echo command to print the message
Use the sleep command to pause for 1 second between prints
Print the message repeatedly until the script is stopped manually
💡 Why This Matters
🌍 Real World
Infinite loops are used in scripts that monitor system status, wait for events, or run servers that need to stay active until stopped.
💼 Career
Understanding infinite loops is important for automation, system administration, and writing scripts that handle continuous tasks or services.
Progress0 / 4 steps
1
Create an infinite loop using while true
Write a Bash script that starts an infinite loop using while true; do and ends it with done. This loop will run forever.
Bash Scripting
Need a hint?

Use while true; do to start the loop and done to end it. The colon : is a placeholder command that does nothing.

2
Add a message to print inside the loop
Inside the infinite loop, add the command echo "Hello, world!" to print the message every time the loop runs.
Bash Scripting
Need a hint?

Use echo "Hello, world!" inside the loop to print the message.

3
Pause the loop for 1 second between prints
Add the command sleep 1 inside the loop after the echo command to pause the script for 1 second between each message.
Bash Scripting
Need a hint?

Use sleep 1 to pause the loop for one second.

4
Run the script and observe the output
Run the complete script. It should print Hello, world! every second repeatedly. Use Ctrl+C to stop the script when you want.
Bash Scripting
Need a hint?

Run the script in your terminal. You will see the message printed every second until you stop it.