How do you exit a loop early from inside a nested loop or a pipeline?
IntermediateConceptArrays, conditionals and loops
Answer
break N and continue N take the number of enclosing loops to affect:
for region in ap-south-1 us-east-1; do
for az in a b c; do
if az_unavailable "$region$az"; then continue 2; fi # next region
deploy "$region$az" || break 2 # stop everything
done
done
Inside cmd | while ..., break only leaves the subshell's loop; exit exits only the subshell. Use process substitution if you need to stop the whole script from inside the loop.
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerWhat is the difference between `"${arr[@]}"` and `"${arr[*]}"`?IntermediateHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?Intermediate