2

Anyone know of a simple way to immediately exit the current iteration of a while/until loop in bash, but continue to the next iteration in the loop?

My basic structure is:

sort -u -r -g -k 5 -t ' ' $filteredtoptrigsf |\
while read -a linA; do

 stuff
 morestuff

 [[ $smthStinks -eq 1 ]] && break

 [[ ${linA[*]} == $onlyThisLineStinks ]]  &&  done

 lotsaSlowProcessing

 doSmthDangerous

done

In the first [[ ... ]] test I definitely want break out of the while loop and never come back, due to some calculated conditions.

In the second [[ ]] test a condition is met that should only break out of the cycle once, avoiding further code below it, but continue reading the next line of input and let while continue.

As you can see I tried done there but that only errors with token unexpected.

I would rather not wrap much of the loop's code inside if's and else's since there are actually quite of few terminating conditions, both global and for current cycle only.

1 Answer 1

5

Try continue instead of done. That takes you to the next iteration of the loop.

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

1 Comment

ahh thank you. I knew I was overlooking something simple in the man page

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.