Bash until Loop and Bash until Loop Example

Loops are one of the fundamental concepts of programming languages. Loops are handy when you want to run a series of commands over and over again until a specific condition is met.

In scripting languages such as Bash, loops are useful for automating repetitive tasks. There are 3 basic loop constructs in Bash scripting, [for]( loop[while]( loop, and until loop.

This tutorial explains the basics of the until loop in Bash.

Bash until Loop

The until loop is used to execute a given set of commands as long as the given condition evaluates to false.

The Bash until loop takes the following form:



The condition is evaluated before executing the commands. If the condition evaluates to false, commands are executed. Otherwise, if the condition evaluates to true the loop will be terminated and the program control will be passed to the command that follows.

In the example below, on each iteration the loop prints the current value of the variablecounterandincrements the variableby one.#!/bin/bash


until [ $counter -gt 5 ]
  echo Counter: $counter


The loop iterates as long as the counter variable has a value greater than four. The script will produce the following output:

Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5

Use the [break]( and [continue]( statements to control the loop execution.

#bash #loop #bash until loop

Bash until Loop and Bash until Loop Example
1.60 GEEK