Exit from a for, while, until, or select loop

      break [n]

If n is supplied, the nth enclosing loop is exited. n must be greater than or equal to 1.

The return status is zero unless n is not greater than or equal to 1.

This is a BASH shell builtin, to display your local syntax from the bash prompt type: help break


for myloop in 1 2 3 4 5 
  echo -n "$myloop"
  if [ "$myloop" -eq 3 ] 
   break # This line will break out of the loop

“Don’t worry about the world coming to an end today. It’s already tomorrow in Australia” ~ Charles Schultz

Related Linux commands

for - Expand words, and execute commands
select - Accept keyboard input.
until - Execute commands (until error).
while - Execute commands.
caller - Return the context of any active subroutine call (a shell function or a script executed with the . or source builtins).
continue - Resume the next iteration of a loop.
Equivalent Windows command: GOTO :eof - Jump to end of file.

Copyright © 1999-2024
Some rights reserved