0
0
Bash Scriptingscripting~5 mins

until loop in Bash Scripting

Choose your learning style9 modes available
Introduction
An until loop repeats commands until a condition becomes true. It helps run tasks while waiting for something to happen.
When you want to keep trying a command until it succeeds.
When you want to wait for a file to appear before continuing.
When you want to repeat a task until a certain number is reached.
When you want to check a condition and run commands until it changes.
Syntax
Bash Scripting
until [ condition ]
do
  commands
 done
The loop runs while the condition is false.
Once the condition becomes true, the loop stops.
Examples
This loop prints the count from 1 to 3.
Bash Scripting
count=1
until [ $count -gt 3 ]
do
  echo "Count is $count"
  count=$((count + 1))
done
This loop waits until the ping command succeeds.
Bash Scripting
until ping -c 1 example.com
 do
  echo "Waiting for network..."
  sleep 2
done
Sample Program
This script prints numbers from 1 to 5 using an until loop.
Bash Scripting
#!/bin/bash

count=1
until [ $count -gt 5 ]
do
  echo "Number: $count"
  count=$((count + 1))
done
OutputSuccess
Important Notes
Make sure the condition will eventually become true to avoid infinite loops.
Use clear and simple conditions for easier understanding.
You can use break inside the loop to stop it early if needed.
Summary
An until loop runs commands until a condition is true.
It is useful for repeating tasks while waiting for something.
The loop stops as soon as the condition becomes true.