• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

What Is The Bash Continue Statement And How To Use It?

CJ Saathoff

Published on: May 28, 2024

Categories: Linux Command Line 0

Shell Scripting is a way to take shell commands that you could use from a normal command line and string them together allowing it to be run as a program allowing for automating repetitive tasks. Bash scripting is just one shell scripting language. Be aware that shell scripting is not the same as traditional programming languages, rather it’s one type of language called scripting languages. Scripting unlike traditional programming doesn’t need to be built/compiled after being created. Scripts are also typically less complex than full-blown software applications. In this blog post, we will touch on loops in bash scripting and move on to how bash continue statements can be used in loops.

Table of Contents
  • What Is A Bash Loop?
  • The Bash Continue Statement
  • Examples
  • Conclusion
  • Additional Links

What Is A Bash Loop?

In programming a loop is used to execute a specific code block repeatedly. During loop execution, the loop iterates over the information passed to it. The same goes for bash loops which allow for specific code to loop through to perform specified tasks.

The Bash Continue Statement

The bash continue statement is used within loops to skip the current iteration of the loop before the loop starts over again. It should be noted that the bash continue statement can only be used in for, while, and until loops.

Examples

The following example scripts include a mix of loops but all include the continue statement.

Wait For File To Exist

#!/bin/bash
until [ -f file.txt ]
	do
		continue
	done
echo "File found!"

This example waits till a file with the file name of file.txt is created and prints out a message.

Wait Until A Set Time Is Reached

#!/bin/bash
target_time=$(date -d "12:00" +%s)
until [ $(date +%s) -ge "$target_time" ]
	do
		sleep 1
		continue
	done
echo "Target time reached!"

This example waits for noon and then prints out a message.

Wait For The Ping To Fail

#!/bin/bash
while true; do
        if ping -c 1 google.com > /dev/null
        then
                continue
        else
                echo "Ping Failed check internet connectivity"
        fi	
        sleep 10
done

This example is a ping check that when fails prints out a message.

Did you notice this is also used nested loop? Although the original loop is a while loop there is a nested if loop inside it. From this point onward the rest of the examples have at least one nested loop.

List Files In the Correct Directory

#!/bin/bash
for f in $(ls); do
    if [ ! -f $f ]; then
        continue
    fi
    echo "File located: $f"
done

This example uses the ls command in the current directory and using the test command prints out the names of files.

Disk Usage Warning

#!/bin/bash
for dir in /home/*
do
        if [ ! -d "$dir" ]
        then
                continue
        fi

        disk_usage=$(du -sh $dir | cut -f1)
        if [ ${disk_usage%G} -gt 80 ]
        then
                echo "Disk usage for $dir is high: $disk_usage"
        fi
done

This example waits for disk usage to reach 80% and then prints out a warning.

Conclusion

When bash scripting loops are invaluable for automating repetitive tasks. The bash continue statement can only be used in loops. The continue statement skips the current iteration of the loop. The next iteration of the loop will then kick off and run through again testing the specified conditions.

If you creating a bash script with loops the continue statement may be just what you need to allow for better loop control.

Additional Links

Looking for more things to read why not check these links out?

  • Other Bash Posts
  • Other Scripting Posts
  • Continue Man Page
CJ Saathoff

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.

Related Posts

Differentiating the df vs du Commands in Linux

Both df and du are Linux commands for estimating disk usage, but they differ in their focus. Df stands for “disk free”, and du stands for disk usage. The first shows the amount of free space available on a volume, and the second shows the amount of space used by specific directories. Both commands have […]

When to Use Here-String vs Heredoc in Bash

Heredoc and Here-string are both ways to input strings into commands that read from stdin. Here's the difference between them.

Resolving the “Fatal: Refusing to Merge Unrelated Histories” Error

The "fatal: refusing to merge unrelated histories" occurs when you try and merge an unrelated repo into your own. Here's what it means.

How to Use Process Substitution in Bash

Bash allows you to combine multiple nested steps in a single command using process substitution. It's faster, and more reusable.

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA