How to Use until Loop in Your Shell Scripts

In bash for, while, and until are three loop constructs. While each loop differs syntactically and functionally their purpose is to iterate over a block of code when a certain expression is evaluated.

Until loop is used to execute a block of code until the expression is evaluated to be false. This is exactly the opposite of a while loop. While loop runs the code block while the expression is true and until loop does the opposite.

until [ expression ]
do
	code block
	...
	...
done

Let’s break down the syntax.

  • To start the loop you should use until keyword followed by an expression within single or double braces.
  • The expression should be evaluated as false until to start running the code block.
  • The actual block of code is placed between do and done.

In this short article, you will learn how to use until loop in your shell scripts using the following examples.

Create an Infinite Loop in Scripts

You can create an infinite loop using a false statement as an expression. When you try to simulate infinite loops try to use sleep which will pass the script periodically.

count=0
until false
do
	echo "Counter = $count"
	((count++))
	sleep 2
done
Create Infinite Loop in Linux
Create Infinite Loop in Linux

Create Single Line Statements

You can create single-line loop statements. Take a look at the below code. This is the same as our first infinite loop example but in a single line. Here you have to use a semicolon (;) to terminate each statement.

# until false; do echo "Counter = $count"; ((count++)); sleep 2; done
Single Line Statement
Single Line Statement

Alter Flow with break and continue Statement

You can use a break and continue statements inside while loop. The break statement will exit out of the loop and will pass the control to the next statement while the continue statement will skip the current iteration and start the next iteration in the loop.

I am using the same infinite loop example. Here when the count is equal to five continue statement will jump over to the next iteration skipping the rest of the loop body. Similarly, the loop breaks when the count is equal to or greater than 10.

count=0
until false
do
  ((count++))
  if [[ $count -eq 5 ]]
  then
    continue
  elif [[ $count -ge 10 ]]
  then
    break
  fi
  echo "Counter = $count"
done
Break and Continue Statement
Break and Continue Statement

That’s it for this article. We will catch you with another interesting article soon ‘until‘ then keep reading and keep supporting us.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Karthick
A passionate software engineer who loves to explore new technologies. He is a public speaker and loves writing about technology, especially about Linux and open source.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.