Skip to content
Home » The Ins and Outs of While and Until Loops in Bash

The Ins and Outs of While and Until Loops in Bash

What is the Difference Between While and Until Loops?

At first glance, while loops and until loops may seem similar. Both loops allow you to execute a block of code multiple times, based on a given condition. However, there is one key difference between the two: the condition at which the loop will terminate.

While loops will continue to execute as long as the given condition is true. On the other hand, until loops will continue to execute as long as the given condition is false. Confused? Let’s take a look at an example to clarify.

# While loop example
i=1
while [ $i -le 5 ]
do
  echo "While loop iteration: $i"
  ((i++))
done

# Output:
# While loop iteration: 1
# While loop iteration: 2
# While loop iteration: 3
# While loop iteration: 4
# While loop iteration: 5
# Until loop example
i=1
until [ $i -gt 5 ]
do
  echo "Until loop iteration: $i"
  ((i++))
done

# Output:
# Until loop iteration: 1
# Until loop iteration: 2
# Until loop iteration: 3
# Until loop iteration: 4
# Until loop iteration: 5

As you can see, the while loop continues to execute as long as the value of $i is less than or equal to 5, while the until loop continues to execute as long as the value of $i is not greater than 5.

Surprising Fact

Did you know that you can actually use the break and continue statements within both while loops and until loops? These statements allow you to exit the loop prematurely or skip to the next iteration, respectively. This can come in handy if you need to add some extra control to your loops.

Key Takeaways

  • While loops execute a block of code as long as the given condition is true
  • Until loops execute a block of code as long as the given condition is false
  • Both while loops and until loops can use the break and continue statements to add extra control

5 Examples or Tips for Increased Productivity

  1. Use a while loop to continuously check for the existence of a file, and perform an action once the file is found. For example: while [ ! -f /tmp/itvraag.nl ] do sleep 1 done echo "File found!"
  2. Use an until loop to continuously ping a website and alert the user once the website becomes unavailable. For example: until ping -c 1 itvraag.nl do echo "Website is down, trying again in 5 seconds..." sleep 5 done echo "Website is up!"
  3. Use a while loop to continuously monitor the output of a command and perform an action based on the output. For example: while true do output=$(cat /proc/loadavg) if [[ $output > 1 ]] then echo "High load detected, taking action..." # perform action here fi sleep 5 done
  4. Use an until loop to continuously check for user input and exit the loop once the correct input is received. For example: until [ "$answer" = "yes" ] do read -p "Do you agree to the terms and conditions? (yes/no) " answer done echo "Thank you for agreeing to the terms and conditions."
  5. Use a while loop in conjunction with the read command to process a file line by line. For example: while read line do echo "Processing line: $line" # perform action on $line here done < /path/to/file.txt

Challenge

Try writing a Bash script that uses both a while loop and an until loop. The script should continuously ask the user for a number between 1 and 10, and exit the loop once the correct number is entered. Use the break statement to exit the loop in both cases.

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen + 5 =