Loop Controls

A loop can be stopped or skipped using either:

 

  • break
  • continue

 

The break command will stop the loop when a specific condition is met.

 

Alternatively, the loop can be made to continue past a specific condition, using the continue command.

 

#!/bin/bash

while :
do
	if [ $i = 17 ]
	then
		break
	fi
	echo $i
	((i++))
done

for i in {6..22}
do
	if [ $i = 17 ]
	then
		continue
	fi
	echo $i
	((++i))
done