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

In this guide, we will touch on loops in bash scripting and move on to how bash continue statements can be used in loops.

On this page
  1. What Is A Bash Loop?
  2. The Bash Continue Statement
  3. Examples
  4. Conclusion
  5. Additional Links

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.

#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

Code
#!/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

Code
#!/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

Code
#!/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

Code
#!/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

Code
#!/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.

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

Illustrated avatar of CJ Saathoff
Written by

CJ Saathoff

CJ Saathoff writes for the NameHero blog.

More from CJ →