bash break for looop
break [n] #Exit optional given n number of loops
bash break for looop
break [n] #Exit optional given n number of loops
bash break
break [n]
[n] is an optional argument and must be greater than or equal to 1.
When [n] is provided, the n-th enclosing loop is exited.
break 1 is equivalent to break.
### eg 1 ############
i=0
while [[ $i -lt 5 ]]
do
echo "Number: $i"
((i++))
if [[ $i -eq 2 ]]; then
break
fi
done
echo 'All Done!'
OUTPUT:
Number: 0
Number: 1
All Done!
## eg 2 #######################
for i in {1..3}; do
for j in {1..3}; do
if [[ $j -eq 2 ]]; then
break
fi
echo "j: $j"
done
echo "i: $i"
done
echo 'All Done!'
OUTPUT:
j: 1
i: 1
j: 1
i: 2
j: 1
i: 3
All Done!
## eq 3 #####################
for i in {1..3}; do
for j in {1..3}; do
if [[ $j -eq 2 ]]; then
break 2 #### see here
fi
echo "j: $j"
done
echo "i: $i"
done
echo 'All Done!'
OUTPUT :
j: 1
All Done!
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us