1

I decide to play around loop just to understand how loops work in bash. But I am the one who got played. All I know is the the break call helps to stop a particular loop

echo "For loop begins"
for (( a = 1; a < 10; a++ ))
do
        echo -e "\tOuter loop ${a}"
        for (( b = 1; b < 100; b++ ))
        do
                if [[ ${b} -gt 5 ]]
                then
                        break 2
                fi
                echo -e "\t\tInner loop ${b}"
        done
done
echo "For loop completed"

So fortunate that the break 2 call also breaks out of the outer loop.

I already tried cahanging my for loop to for (( a = 1; a <= 10; a++ )) and the second one to for (( b = 1; b <= 100; b++ )) putting <= instead of < but I still get same output.

genius@GeniusDPhil-hp250g1notebookpc:~$ ./loop.sh 
For loop begins
        Outer loop 1
                Inner loop 1
                Inner loop 2
                Inner loop 3
                Inner loop 4
                Inner loop 5
For loop completed
genius@GeniusDPhil-hp250g1notebookpc:~$ 

I was expecting the outer loop to run 10 times, does break also take me out of my outer loop? if yes how do I use break properly? if no the what's the issue?

NOTE: The programmer in me is 3 months old.

6
  • The outer loop cannot run 10 times because there are only 9 integers between 1 included and 10 excluded: 1, 2, ..., 9. Moreover, as you break the two loops with break 2, only 5 iterations of the inner loop, and one iteration of the outer loop will be executed. You forgot to explain what you want (please edit your question to add this) but if you want to break only the inner loop use break 1 (or just break, it's the same). Commented Sep 27, 2023 at 4:59
  • From bash manual: break [n]: Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be >= 1. If n is greater than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless n is not greater than or equal to 1. Commented Sep 27, 2023 at 5:00
  • Note: you could use the simpler arithmetic compound command (( ...)) for your arithmetic test: if (( b > 5 )); then..., instead of the [[ ${b} -gt 5 ]] conditional expression. Commented Sep 27, 2023 at 5:05
  • The nesting of the if [[ ${b} -gt 5 ]]; then ... fi does not count as a loop level. break n means break out of n nesting levels of looping, not n nesting levels of loops or other kinds of statements. The default n is 1: break out of the inner-most enclosing loop. break 2 means break out of the innermost enclosing loop and the one enclosing it, and so on. Commented Sep 27, 2023 at 5:28
  • How did you arrive at break 2 before learning about just break? It's odd that the only thing wrong is that we have to delete the 2 in your script; what led you to add that? Commented Sep 27, 2023 at 5:29

1 Answer 1

0

What I would do:

echo "For loop begins"
for (( a = 1; a <= 10; a++ ))
do
        echo -e "\tOuter loop ${a}"
        for (( b = 1; b < 100; b++ ))
        do
                if [[ ${b} -gt 5 ]]
                then
                        break
                fi
                echo -e "\t\tInner loop ${b}"
        done
done
echo "For loop completed"

Note the use of break only, because break 2 break the outer loop too. The logic is wrong from your code if you need to iterate 10 times like I does here.

Check

man bash | less +/'^ +break'

break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n iq greater than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless n is not greater than or equal to 1.


Using modern , better use if (( b > 5 ))


((...)) and $((...)) are arithmetic commands, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed.

See http://mywiki.wooledge.org/ArithmeticExpression

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, resource and fast response. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.