• Pricing
DashboardContact Sales
  • Blogs
  • Discover
  • Docs
  • Community
  • AgentBeta
⌘K

Over 80,000,000 Cloud Servers Launched

Over 80,000,000 Cloud Servers Launched

Cloud ComputeCloud GPUBare MetalFile SystemObject StorageBlock StorageManaged DatabasesCDNServerlessKubernetesContainer RegistryDirect ConnectLoad Balancers
RegionsAdvanced NetworkControl PanelOperating SystemsUpload ISO
Industry CloudOne-Click DeploymentUse Cases
Browse AppsBecome a Vendor
FAQDevelopers / APIsVultr DocsServer StatusBug BountyPromotionsSolution PartnersStart-Up Programs
Our TeamNewsBrand AssetsReferral ProgramCreator ProgramCareersSLALegalVultr Trust CenterContactYour Privacy ChoicesSubprocessorsAccessibility
Contact SalesSign Up

© Vultr 2026 | VULTR is a registered trademark of The Constant Company, LLC.

Terms of ServiceAUPDMCAPrivacy PolicyCookie Policy

In this Article

The Short Answer VersionUse the break Command in a for LoopUse the break Command in a while LoopUse the break Command in an until LoopBreaking Out of Nested LoopsConclusion
  1. Docs
  2. Articles & Guides
  3. Development

How to Use the Break Command in Bash

4 min readUpdated 31 July 2025
0 comments
How to Use the Break Command in Bash header image

You can create Bash scripts to manage repetitive tasks efficiently by using loops like for, while, and until. In some cases, you may need to exit a loop early, such as when a condition is met, an input is received, or further execution is unnecessary. The break command lets you exit the loop immediately, giving you control over script flow.

This article explains how to use the break command inside for, while, and until loops in Bash, using clear and focused examples that demonstrate how loop control works in each case.

The Short Answer Version

Here’s a quick summary of how the break command behaves in different loop types:


# For loop with break: Stops when n equals 3

    for n in {1..5}; do
        [ "$n" -eq 3 ] && break
        echo "n = $n"
    done

# While loop with break: Stops when user types "stop"

    while true; do
        read -p "Type something: " user_input
        [ "$user_input" == "stop" ] && break
        echo "You typed: $user_input"
    done

# Until loop with break: Stops when index reaches 6

    index=1
    until [ "$index" -gt 10 ]; do
        [ "$index" -eq 6 ] && break
        echo "Index: $index"
        ((index++))
    done

The next sections explain how to use the break command inside each loop type in more detail.

Use the break Command in a for Loop

A for loop is useful when you know how many times the loop should run. If a condition arises during execution that makes continuing unnecessary, such as reaching a specific value, you can use the break command to exit the loop early.

Command Syntax

for variable in list; do
    [condition] && break
done

Command Demonstration

This example prints numbers from 1 to 10 but exits early when the value reaches 5.

BASH

#!/bin/bash

echo "Beginning number loop..."

for num in {1..10}; do
    echo "Current: $num"
    if [ "$num" -eq 5 ]; then
        echo "Reached target number. Leaving."
        break
    fi
done

echo "Loop finished."
Powered by Vultr AgentBeta

Output:

Beginning number loop...
Current: 1
Current: 2
Current: 3
Current: 4
Current: 5
Reached target number. Leaving.
Loop finished.

Use the break Command in a while Loop

A while loop is ideal when you don't know in advance how many times the loop should run. You can use the break command to exit the loop based on user input or other dynamic conditions.

Command Syntax

while [condition]; do
    [condition] && break
done

Command Demonstration

This example accepts user input repeatedly and exits the loop when the user types quit.

BASH

#!/bin/bash

echo "Provide inputs (enter 'quit' to exit):"

while true; do
    read -p "Input: " response
    if [ "$response" == "quit" ]; then
        echo "User requested to quit."
        break
    fi
    echo "Received: $response"
done

echo "Exited while loop."
Powered by Vultr AgentBeta

Output:

Provide inputs (enter 'quit' to exit):
Input: test
Received: test
Input: something
Received: something
Input: quit
User requested to quit.
Exited while loop.

Use the break Command in an until Loop

An until loop runs as long as the given condition remains false. When a specific condition becomes true, such as reaching a limit, you can use the break command to exit the loop early.

Command Syntax

until [condition]; do
    [condition] && break
done

Command Demonstration

This example counts from 1 to 10 but exits the loop early when the counter reaches 5.

BASH

#!/bin/bash

counter=1

echo "Starting counter, will stop at five..."

until [ "$counter" -gt 10 ]; do
    echo "Counter: $counter"
    if [ "$counter" -eq 5 ]; then
        echo "Breaking loop at counter 5"
        break
    fi
    ((counter++))
done

echo "Until loop complete."
Powered by Vultr AgentBeta

Output:

Starting counter, will stop at five...
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Breaking loop at counter 5
Until loop complete.

Breaking Out of Nested Loops

When using nested loops, the break command only exits the innermost loop by default. To exit multiple levels at once, use break N, where N is the number of nested loops to break out of.

Command Syntax

for outer in list; do
    for inner in list; do
        [condition] && break N
    done
done

Command Demonstration

This example prints coordinates from two nested loops and exits both loops when y equals 2.

BASH

#!/bin/bash

for x in {1..3}; do
    for y in {1..3}; do
        echo "x=$x, y=$y"
        if [ "$y" -eq 2 ]; then
            echo "Exiting both loops."
            break 2
        fi
    done
done

echo "Nested loops done."
Powered by Vultr AgentBeta

Output:

x=1, y=1
x=1, y=2
Exiting both loops.
Nested loops done.

Conclusion

In this article, you explored how the break command works in Bash scripting. You learned how to use it to exit early from for, while, and until loops based on specific conditions. You also saw how to break out of multiple nested loops using break N to control complex flow logic.

Tags:Bash

Tech Talk: Running a Self-Improving AI Agent with Hermes on Vultr

Have a suggestion or found an issue with this article?

About the Author

AS

Adarsh Sojitra

View Profile
View Profile

More to Explore

Show More
tech-talk-thumbnail

Tech Talk: Running a Self-Improving AI Agent with Hermes on Vultr

tech-talk-thumbnail

Tech Talk: Implementing Fine-Grained Access Control with Vultr IAM

tech-talk-thumbnail

Tech Talk: Getting Started with Vultr IAM

tech-talk-thumbnail

Tech Talk: Simplifying Zero-Trust Networking with NetBird on Vultr Marketplace

Show More

0 Comments

Start the conversation

Be the first to comment and share your perspective with the community.