1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ============================================================================
// BUILTIN-009: exit command (POSIX builtin)
// ============================================================================
// Task: Document exit (terminate shell) builtin command
// Reference: GNU Bash Manual Section 4.1 (Bourne Shell Builtins)
// POSIX: exit is POSIX-COMPLIANT (SUPPORTED)
//
// Syntax:
// exit [n]
// exit 0 # Exit with success (status 0)
// exit 1 # Exit with failure (status 1)
// exit # Exit with status of last command ($?)
// exit $? # Explicit exit with last command status
//
// POSIX Compliance:
// SUPPORTED: exit [n] where n is 0-255
// SUPPORTED: exit with no args (uses $? from last command)
// SUPPORTED: Exit status 0 = success, non-zero = failure
// SUPPORTED: In functions, exit terminates entire script (not just function)
// SUPPORTED: In subshells, exit terminates only the subshell
//
// Exit Status Conventions (POSIX):
// 0: Success (command completed successfully)
// 1: General errors (catchall for miscellaneous errors)
// 2: Misuse of shell builtins (missing keyword or command)
// 126: Command invoked cannot execute (permission problem)
// 127: Command not found (illegal command)
// 128: Invalid argument to exit (non-numeric or out of range)
// 128+N: Fatal error signal N (e.g., 130 = 128+2 for SIGINT/Ctrl-C)
// 255: Exit status out of range (exit takes only 0-255)
//
// Bash Extensions:
// exit with value >255: Wraps modulo 256 (exit 256 becomes 0)
// exit with negative value: Wraps modulo 256 (exit -1 becomes 255)
// exit in trap handlers: Specific behaviors in various traps
//
// bashrs Support:
// SUPPORTED: exit [n] where n is 0-255
// SUPPORTED: exit with no args (uses $?)
// SUPPORTED: Standard exit status conventions
// NOT SUPPORTED: exit >255 (bash wrapping behavior)
// NOT SUPPORTED: exit with negative values (bash wrapping behavior)
//
// Rust Mapping:
// exit 0 → std::process::exit(0)
// exit 1 → std::process::exit(1)
// exit $? → std::process::exit(last_exit_status)
// exit → std::process::exit(last_exit_status)
//
// Purified Bash:
// exit 0 → exit 0 (POSIX supported)
// exit 1 → exit 1 (POSIX supported)
// exit → exit (POSIX supported, uses $?)
// exit 256 → exit 0 (normalize to 0-255 range)
// exit -1 → exit 255 (normalize to 0-255 range)
//
// Exit vs Return:
// exit: Terminates entire script (even from function)
// return: Returns from function only (function-local)
// In script: exit terminates script
// In function: exit terminates script, return returns from function
// In subshell: exit terminates subshell only
//
// Common Use Cases:
// 1. Success exit: exit 0 (at end of script)
// 2. Error exit: exit 1 (on error conditions)
// 3. Conditional exit: [ -z "$VAR" ] && exit 1
// 4. Exit with last status: command || exit
// 5. Exit with custom code: exit 2 (for specific error types)
// 6. Early return: if [ error ]; then exit 1; fi
//
// Edge Cases:
// 1. exit with no args → uses $? from last command
// 2. exit >255 → bash wraps modulo 256 (exit 256 = 0)
// 3. exit <0 → bash wraps modulo 256 (exit -1 = 255)
// 4. exit in subshell → terminates subshell only, not parent
// 5. exit in function → terminates entire script, not just function
// 6. exit in trap → depends on trap type (EXIT, ERR, etc.)
//
// Best Practices:
// 1. Use exit 0 for success at end of script
// 2. Use exit 1 for general errors
// 3. Use specific exit codes (2-125) for different error types
// 4. Document exit codes in script header
// 5. Use return (not exit) in functions to avoid terminating script
// 6. Check $? before exit to propagate error codes
// 7. Avoid exit codes >125 (reserved for signals and special meanings)
//
// POSIX vs Bash Comparison:
//
// | Feature | POSIX | Bash | bashrs | Notes |
// |----------------------|-------|------|--------|--------------------------------|
// | exit 0 | ✓ | ✓ | ✓ | Success exit |
// | exit 1 | ✓ | ✓ | ✓ | Error exit |
// | exit [0-255] | ✓ | ✓ | ✓ | Valid exit codes |
// | exit (no args) | ✓ | ✓ | ✓ | Uses $? from last command |
// | exit $? | ✓ | ✓ | ✓ | Explicit last command status |
// | exit >255 | ✗ | ✓ | ✗ | Wraps modulo 256 (bash only) |
// | exit <0 | ✗ | ✓ | ✗ | Wraps modulo 256 (bash only) |
// | Terminates script | ✓ | ✓ | ✓ | From anywhere (incl. functions)|
// | Terminates subshell | ✓ | ✓ | ✓ | Only subshell, not parent |
// | Standard exit codes | ✓ | ✓ | ✓ | 0=success, 1-2=errors, etc. |
//
// ✓ = Supported
// ✗ = Not supported
//
// Summary:
// exit command: POSIX, FULLY SUPPORTED (0-255 range)
// exit terminates script (from anywhere, including functions)
// exit in subshell terminates only subshell
// exit with no args uses $? from last command
// Standard exit codes: 0 (success), 1 (general error), 2 (misuse), 126 (no execute), 127 (not found), 128+N (signal)
// Use exit 0 for success, exit 1 for general errors
// Use return (not exit) in functions to avoid terminating script
// Bash wrapping behavior (>255, <0) is NOT SUPPORTED