Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Break built-in

The break built-in terminates the execution of a loop.

Synopsis

break [n]

Description

break n quits the execution of the nth innermost for, while, or until loop. The specified loop must lexically enclose the break command, that is:

It is an error if there is no loop enclosing the break command. If n is greater than the number of enclosing loops, the built-in exits the outermost one.

Options

None.

Operands

Operand n specifies the nest level of the loop to exit. If omitted, it defaults to 1. It is an error if the value is not a positive decimal integer.

Exit status

Zero if the built-in successfully breaks the loop; non-zero otherwise.

Examples

See Break and continue.

Compatibility

The break built-in is specified by POSIX.1-2024.

The behavior is unspecified in POSIX when the break built-in is used without an enclosing loop, in which case the current implementation returns an error.

POSIX allows the built-in to break a loop running in the current execution environment that does not lexically enclose the break command. Our implementation does not do that.

In some shells, the break built-in lacks support for the -- separator.

Previous versions of yash supported the non-standard -i option, but this is not yet supported in yash-rs.