If you want to exit out of a loop but want the script to continue running from after the loop then you need to use the break statement;
Code:
for (( i=0; i<5; i+=1 )) { | |
if [[ $i -eq 3 ]] | |
then | |
break | |
else | |
echo "i: $i" | |
fi | |
} |
Output:
i: 0
i: 1
i: 2
If you have two loops within each other only the current loop will exit with break. In the following example when the first break is called, when j=0 and i=3, only the second for loop exits and the outer one continues.
Code:
for (( j=0; j<2; j+=1 )) { | |
for (( i=0; i<5; i+=1 )) { | |
if [[ $i -eq 3 ]] | |
then | |
break | |
else | |
echo "j: $j" | |
echo "i: $i" | |
echo "" | |
fi | |
} | |
} |
Output:
j: 0
i: 0j: 0
i: 1j: 0
i: 2j: 1
i: 0j: 1
i: 1j: 1
i: 2