1BREAK(1P)                  POSIX Programmer's Manual                 BREAK(1P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       break — exit from for, while, or until loop
13

SYNOPSIS

15       break [n]
16

DESCRIPTION

18       If n is specified, the break utility shall exit from the nth  enclosing
19       for, while, or until loop. If n is not specified, break shall behave as
20       if n was specified as 1. Execution  shall  continue  with  the  command
21       immediately  following  the  exited  loop. The value of n is a positive
22       decimal integer. If n is greater than the number  of  enclosing  loops,
23       the  outermost enclosing loop shall be exited. If there is no enclosing
24       loop, the behavior is unspecified.
25
26       A loop shall enclose a break or continue command if the loop  lexically
27       encloses  the  command.  A  loop lexically encloses a break or continue
28       command if the command is:
29
30        *  Executing in the same  execution  environment  (see  Section  2.12,
31           Shell Execution Environment) as the compound-list of the loop's do-
32           group (see Section 2.10.2, Shell Grammar Rules), and
33
34        *  Contained in a compound-list associated with the  loop  (either  in
35           the compound-list of the loop's do-group or, if the loop is a while
36           or until loop, in the compound-list following the  while  or  until
37           reserved word), and
38
39        *  Not  in  the  body  of a function whose function definition command
40           (see Section 2.9.5, Function Definition Command) is contained in  a
41           compound-list associated with the loop.
42
43       If  n is greater than the number of lexically enclosing loops and there
44       is a non-lexically enclosing loop in progress  in  the  same  execution
45       environment as the break or continue command, it is unspecified whether
46       that loop encloses the command.
47

OPTIONS

49       None.
50

OPERANDS

52       See the DESCRIPTION.
53

STDIN

55       Not used.
56

INPUT FILES

58       None.
59

ENVIRONMENT VARIABLES

61       None.
62

ASYNCHRONOUS EVENTS

64       Default.
65

STDOUT

67       Not used.
68

STDERR

70       The standard error shall be used only for diagnostic messages.
71

OUTPUT FILES

73       None.
74

EXTENDED DESCRIPTION

76       None.
77

EXIT STATUS

79        0    Successful completion.
80
81       >0    The n value was not an unsigned decimal integer greater  than  or
82             equal to 1.
83

CONSEQUENCES OF ERRORS

85       Default.
86
87       The following sections are informative.
88

APPLICATION USAGE

90       None.
91

EXAMPLES

93       for i in *
94       do
95           if test -d "$i"
96           then break
97           fi
98       done
99
100       The results of running the following example are unspecified: there are
101       two loops in progress when the break command is executed, and they  are
102       in  the  same  execution  environment,  but  neither  loop is lexically
103       enclosing the break command. (There are no  loops  lexically  enclosing
104       the continue commands, either.)
105
106       foo() {
107           for j in 1 2; do
108               echo 'break 2' >/tmp/do_break
109               echo "  sourcing /tmp/do_break ($j)..."
110               # the behavior of the break from running the following command
111               # results in unspecified behavior:
112               . /tmp/do_break
113
114               do_continue() { continue 2; }
115               echo "  running do_continue ($j)..."
116               # the behavior of the continue in the following function call
117               # results in unspecified behavior (if execution reaches this
118               # point):
119               do_continue
120
121               trap 'continue 2' USR1
122               echo "  sending SIGUSR1 to self ($j)..."
123               # the behavior of the continue in the trap invoked from the
124               # following signal results in unspecified behavior (if
125               # execution reaches this point):
126               kill -s USR1 $$
127               sleep 1
128           done
129       }
130       for i in 1 2; do
131           echo "running foo ($i)..."
132           foo
133       done
134

RATIONALE

136       In  early proposals, consideration was given to expanding the syntax of
137       break and continue to refer to a label associated with the  appropriate
138       loop  as a preferable alternative to the n method. However, this volume
139       of POSIX.1‐2017 does reserve the name space  of  command  names  ending
140       with  a  <colon>.  It is anticipated that a future implementation could
141       take advantage of this and provide something like:
142
143
144           outofloop: for i in a b c d e
145           do
146               for j in 0 1 2 3 4 5 6 7 8 9
147               do
148                   if test -r "${i}${j}"
149                   then break outofloop
150                   fi
151               done
152           done
153
154       and that this might be standardized after implementation experience  is
155       achieved.
156

FUTURE DIRECTIONS

158       None.
159

SEE ALSO

161       Section 2.14, Special Built-In Utilities
162
164       Portions  of  this text are reprinted and reproduced in electronic form
165       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
166       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
167       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
168       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
169       event of any discrepancy between this version and the original IEEE and
170       The  Open Group Standard, the original IEEE and The Open Group Standard
171       is the referee document. The original Standard can be  obtained  online
172       at http://www.opengroup.org/unix/online.html .
173
174       Any  typographical  or  formatting  errors that appear in this page are
175       most likely to have been introduced during the conversion of the source
176       files  to  man page format. To report such errors, see https://www.ker
177       nel.org/doc/man-pages/reporting_bugs.html .
178
179
180
181IEEE/The Open Group                  2017                            BREAK(1P)
Impressum