How Do You Stop a Loop in Linux?

How Do You Stop a Loop in Linux?
Contents

If you want ctrl+c to stop the loop, but not terminate the script, you can place || break after whatever command you’re running. As long as the program you’re running terminates on ctrl+c, this works great. If you’re in nested loop, you can use “break 2” to get out of two levels, etc.

How do you exit a loop in Linux?

The break statement is used to exit the current loop. The continue statement is used to exit the current iteration of a loop and begin the next iteration.

How do I end a loop?

Tips

  1. The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement.
  2. break is not defined outside a for or while loop. To exit a function, use return .
James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.