| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Exit Codes With Special Meanings</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="PREVIOUS" |
| 14 | TITLE="Parsing and Managing Pathnames" |
| 15 | HREF="pathmanagement.html"><LINK |
| 16 | REL="NEXT" |
| 17 | TITLE="A Detailed Introduction to I/O and I/O Redirection" |
| 18 | HREF="ioredirintro.html"><META |
| 19 | HTTP-EQUIV="Content-Style-Type" |
| 20 | CONTENT="text/css"><LINK |
| 21 | REL="stylesheet" |
| 22 | HREF="common/kde-common.css" |
| 23 | TYPE="text/css"><META |
| 24 | HTTP-EQUIV="Content-Type" |
| 25 | CONTENT="text/html; charset=iso-8859-1"><META |
| 26 | HTTP-EQUIV="Content-Language" |
| 27 | CONTENT="en"><LINK |
| 28 | REL="stylesheet" |
| 29 | HREF="common/kde-localised.css" |
| 30 | TYPE="text/css" |
| 31 | TITLE="KDE-English"><LINK |
| 32 | REL="stylesheet" |
| 33 | HREF="common/kde-default.css" |
| 34 | TYPE="text/css" |
| 35 | TITLE="KDE-Default"></HEAD |
| 36 | ><BODY |
| 37 | CLASS="APPENDIX" |
| 38 | BGCOLOR="#FFFFFF" |
| 39 | TEXT="#000000" |
| 40 | LINK="#AA0000" |
| 41 | VLINK="#AA0055" |
| 42 | ALINK="#AA0000" |
| 43 | STYLE="font-family: sans-serif;" |
| 44 | ><DIV |
| 45 | CLASS="NAVHEADER" |
| 46 | ><TABLE |
| 47 | SUMMARY="Header navigation table" |
| 48 | WIDTH="100%" |
| 49 | BORDER="0" |
| 50 | CELLPADDING="0" |
| 51 | CELLSPACING="0" |
| 52 | ><TR |
| 53 | ><TH |
| 54 | COLSPAN="3" |
| 55 | ALIGN="center" |
| 56 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 57 | ></TR |
| 58 | ><TR |
| 59 | ><TD |
| 60 | WIDTH="10%" |
| 61 | ALIGN="left" |
| 62 | VALIGN="bottom" |
| 63 | ><A |
| 64 | HREF="pathmanagement.html" |
| 65 | ACCESSKEY="P" |
| 66 | >Prev</A |
| 67 | ></TD |
| 68 | ><TD |
| 69 | WIDTH="80%" |
| 70 | ALIGN="center" |
| 71 | VALIGN="bottom" |
| 72 | ></TD |
| 73 | ><TD |
| 74 | WIDTH="10%" |
| 75 | ALIGN="right" |
| 76 | VALIGN="bottom" |
| 77 | ><A |
| 78 | HREF="ioredirintro.html" |
| 79 | ACCESSKEY="N" |
| 80 | >Next</A |
| 81 | ></TD |
| 82 | ></TR |
| 83 | ></TABLE |
| 84 | ><HR |
| 85 | ALIGN="LEFT" |
| 86 | WIDTH="100%"></DIV |
| 87 | ><DIV |
| 88 | CLASS="APPENDIX" |
| 89 | ><H1 |
| 90 | ><A |
| 91 | NAME="EXITCODES" |
| 92 | ></A |
| 93 | >Appendix E. Exit Codes With Special Meanings</H1 |
| 94 | ><P |
| 95 | ><A |
| 96 | NAME="EXITCODESREF" |
| 97 | ></A |
| 98 | ></P |
| 99 | ><DIV |
| 100 | CLASS="TABLE" |
| 101 | ><HR><A |
| 102 | NAME="AEN23549" |
| 103 | ></A |
| 104 | ><P |
| 105 | ><B |
| 106 | >Table E-1. <I |
| 107 | CLASS="FIRSTTERM" |
| 108 | >Reserved</I |
| 109 | > Exit Codes</B |
| 110 | ></P |
| 111 | ><TABLE |
| 112 | BORDER="1" |
| 113 | CLASS="CALSTABLE" |
| 114 | ><THEAD |
| 115 | ><TR |
| 116 | ><TH |
| 117 | ALIGN="LEFT" |
| 118 | VALIGN="TOP" |
| 119 | >Exit Code Number</TH |
| 120 | ><TH |
| 121 | ALIGN="LEFT" |
| 122 | VALIGN="TOP" |
| 123 | >Meaning</TH |
| 124 | ><TH |
| 125 | ALIGN="LEFT" |
| 126 | VALIGN="TOP" |
| 127 | >Example</TH |
| 128 | ><TH |
| 129 | ALIGN="LEFT" |
| 130 | VALIGN="TOP" |
| 131 | >Comments</TH |
| 132 | ></TR |
| 133 | ></THEAD |
| 134 | ><TBODY |
| 135 | ><TR |
| 136 | ><TD |
| 137 | ALIGN="LEFT" |
| 138 | VALIGN="TOP" |
| 139 | ><TT |
| 140 | CLASS="OPTION" |
| 141 | >1</TT |
| 142 | ></TD |
| 143 | ><TD |
| 144 | ALIGN="LEFT" |
| 145 | VALIGN="TOP" |
| 146 | >Catchall for general errors</TD |
| 147 | ><TD |
| 148 | ALIGN="LEFT" |
| 149 | VALIGN="TOP" |
| 150 | >let "var1 = 1/0"</TD |
| 151 | ><TD |
| 152 | ALIGN="LEFT" |
| 153 | VALIGN="TOP" |
| 154 | >Miscellaneous errors, such as <SPAN |
| 155 | CLASS="QUOTE" |
| 156 | >"divide by |
| 157 | zero"</SPAN |
| 158 | > and other impermissible operations</TD |
| 159 | ></TR |
| 160 | ><TR |
| 161 | ><TD |
| 162 | ALIGN="LEFT" |
| 163 | VALIGN="TOP" |
| 164 | ><TT |
| 165 | CLASS="OPTION" |
| 166 | >2</TT |
| 167 | ></TD |
| 168 | ><TD |
| 169 | ALIGN="LEFT" |
| 170 | VALIGN="TOP" |
| 171 | >Misuse of shell builtins (according to Bash documentation)</TD |
| 172 | ><TD |
| 173 | ALIGN="LEFT" |
| 174 | VALIGN="TOP" |
| 175 | >empty_function() {}</TD |
| 176 | ><TD |
| 177 | ALIGN="LEFT" |
| 178 | VALIGN="TOP" |
| 179 | ><A |
| 180 | HREF="debugging.html#MISSINGKEYWORD" |
| 181 | >Missing keyword</A |
| 182 | > |
| 183 | or command, or permission problem (and <A |
| 184 | HREF="filearchiv.html#DIFFERR2" |
| 185 | ><I |
| 186 | CLASS="FIRSTTERM" |
| 187 | >diff</I |
| 188 | > return code |
| 189 | on a failed binary file comparison</A |
| 190 | >).</TD |
| 191 | ></TR |
| 192 | ><TR |
| 193 | ><TD |
| 194 | ALIGN="LEFT" |
| 195 | VALIGN="TOP" |
| 196 | ><TT |
| 197 | CLASS="OPTION" |
| 198 | >126</TT |
| 199 | ></TD |
| 200 | ><TD |
| 201 | ALIGN="LEFT" |
| 202 | VALIGN="TOP" |
| 203 | >Command invoked cannot execute</TD |
| 204 | ><TD |
| 205 | ALIGN="LEFT" |
| 206 | VALIGN="TOP" |
| 207 | >/dev/null</TD |
| 208 | ><TD |
| 209 | ALIGN="LEFT" |
| 210 | VALIGN="TOP" |
| 211 | >Permission problem or command is not an executable</TD |
| 212 | ></TR |
| 213 | ><TR |
| 214 | ><TD |
| 215 | ALIGN="LEFT" |
| 216 | VALIGN="TOP" |
| 217 | ><TT |
| 218 | CLASS="OPTION" |
| 219 | >127</TT |
| 220 | ></TD |
| 221 | ><TD |
| 222 | ALIGN="LEFT" |
| 223 | VALIGN="TOP" |
| 224 | ><SPAN |
| 225 | CLASS="QUOTE" |
| 226 | >"command not found"</SPAN |
| 227 | ></TD |
| 228 | ><TD |
| 229 | ALIGN="LEFT" |
| 230 | VALIGN="TOP" |
| 231 | >illegal_command</TD |
| 232 | ><TD |
| 233 | ALIGN="LEFT" |
| 234 | VALIGN="TOP" |
| 235 | >Possible problem with <TT |
| 236 | CLASS="VARNAME" |
| 237 | >$PATH</TT |
| 238 | > or a typo</TD |
| 239 | ></TR |
| 240 | ><TR |
| 241 | ><TD |
| 242 | ALIGN="LEFT" |
| 243 | VALIGN="TOP" |
| 244 | ><TT |
| 245 | CLASS="OPTION" |
| 246 | >128</TT |
| 247 | ></TD |
| 248 | ><TD |
| 249 | ALIGN="LEFT" |
| 250 | VALIGN="TOP" |
| 251 | >Invalid argument to <A |
| 252 | HREF="exit-status.html#EXITCOMMANDREF" |
| 253 | >exit</A |
| 254 | ></TD |
| 255 | ><TD |
| 256 | ALIGN="LEFT" |
| 257 | VALIGN="TOP" |
| 258 | >exit 3.14159</TD |
| 259 | ><TD |
| 260 | ALIGN="LEFT" |
| 261 | VALIGN="TOP" |
| 262 | ><B |
| 263 | CLASS="COMMAND" |
| 264 | >exit</B |
| 265 | > takes only integer args in the |
| 266 | range <SPAN |
| 267 | CLASS="RETURNVALUE" |
| 268 | >0 - 255</SPAN |
| 269 | > (see |
| 270 | first footnote)</TD |
| 271 | ></TR |
| 272 | ><TR |
| 273 | ><TD |
| 274 | ALIGN="LEFT" |
| 275 | VALIGN="TOP" |
| 276 | ><TT |
| 277 | CLASS="OPTION" |
| 278 | >128+n</TT |
| 279 | ></TD |
| 280 | ><TD |
| 281 | ALIGN="LEFT" |
| 282 | VALIGN="TOP" |
| 283 | >Fatal error signal <SPAN |
| 284 | CLASS="QUOTE" |
| 285 | >"n"</SPAN |
| 286 | ></TD |
| 287 | ><TD |
| 288 | ALIGN="LEFT" |
| 289 | VALIGN="TOP" |
| 290 | ><I |
| 291 | CLASS="FIRSTTERM" |
| 292 | >kill -9</I |
| 293 | > <TT |
| 294 | CLASS="VARNAME" |
| 295 | >$PPID</TT |
| 296 | > of script</TD |
| 297 | ><TD |
| 298 | ALIGN="LEFT" |
| 299 | VALIGN="TOP" |
| 300 | ><TT |
| 301 | CLASS="USERINPUT" |
| 302 | ><B |
| 303 | >$?</B |
| 304 | ></TT |
| 305 | > returns |
| 306 | <SPAN |
| 307 | CLASS="ERRORCODE" |
| 308 | >137</SPAN |
| 309 | > (128 + 9)</TD |
| 310 | ></TR |
| 311 | ><TR |
| 312 | ><TD |
| 313 | ALIGN="LEFT" |
| 314 | VALIGN="TOP" |
| 315 | ><TT |
| 316 | CLASS="OPTION" |
| 317 | >130</TT |
| 318 | ></TD |
| 319 | ><TD |
| 320 | ALIGN="LEFT" |
| 321 | VALIGN="TOP" |
| 322 | >Script terminated by Control-C</TD |
| 323 | ><TD |
| 324 | ALIGN="LEFT" |
| 325 | VALIGN="TOP" |
| 326 | ><SPAN |
| 327 | CLASS="emphasis" |
| 328 | ><I |
| 329 | CLASS="EMPHASIS" |
| 330 | >Ctl-C</I |
| 331 | ></SPAN |
| 332 | ></TD |
| 333 | ><TD |
| 334 | ALIGN="LEFT" |
| 335 | VALIGN="TOP" |
| 336 | >Control-C is fatal error signal |
| 337 | <SPAN |
| 338 | CLASS="ERRORCODE" |
| 339 | >2</SPAN |
| 340 | >, (130 = 128 + 2, see above)</TD |
| 341 | ></TR |
| 342 | ><TR |
| 343 | ><TD |
| 344 | ALIGN="LEFT" |
| 345 | VALIGN="TOP" |
| 346 | ><TT |
| 347 | CLASS="OPTION" |
| 348 | >255*</TT |
| 349 | ></TD |
| 350 | ><TD |
| 351 | ALIGN="LEFT" |
| 352 | VALIGN="TOP" |
| 353 | >Exit status out of range</TD |
| 354 | ><TD |
| 355 | ALIGN="LEFT" |
| 356 | VALIGN="TOP" |
| 357 | >exit <SPAN |
| 358 | CLASS="RETURNVALUE" |
| 359 | >-1</SPAN |
| 360 | ></TD |
| 361 | ><TD |
| 362 | ALIGN="LEFT" |
| 363 | VALIGN="TOP" |
| 364 | ><B |
| 365 | CLASS="COMMAND" |
| 366 | >exit</B |
| 367 | > takes only integer args in the |
| 368 | range <SPAN |
| 369 | CLASS="ERRORCODE" |
| 370 | >0 - 255</SPAN |
| 371 | ></TD |
| 372 | ></TR |
| 373 | ></TBODY |
| 374 | ></TABLE |
| 375 | ><HR></DIV |
| 376 | ><P |
| 377 | >According to the above table, exit codes <SPAN |
| 378 | CLASS="ERRORCODE" |
| 379 | >1 - 2, |
| 380 | 126 - 165, and 255</SPAN |
| 381 | > |
| 382 | |
| 383 | <A |
| 384 | NAME="AEN23629" |
| 385 | HREF="#FTN.AEN23629" |
| 386 | >[1]</A |
| 387 | > |
| 388 | |
| 389 | have special meanings, and should therefore be avoided for |
| 390 | user-specified exit parameters. Ending a script with <I |
| 391 | CLASS="FIRSTTERM" |
| 392 | >exit |
| 393 | 127</I |
| 394 | > would certainly cause confusion when troubleshooting |
| 395 | (is the error code a <SPAN |
| 396 | CLASS="QUOTE" |
| 397 | >"command not found"</SPAN |
| 398 | > or a |
| 399 | user-defined one?). However, many scripts use an <I |
| 400 | CLASS="FIRSTTERM" |
| 401 | >exit |
| 402 | 1</I |
| 403 | > as a general bailout-upon-error. Since exit code |
| 404 | <SPAN |
| 405 | CLASS="ERRORCODE" |
| 406 | >1</SPAN |
| 407 | > signifies so many possible errors, |
| 408 | it is not particularly useful in debugging.</P |
| 409 | ><P |
| 410 | ><A |
| 411 | NAME="SYSEXITSREF" |
| 412 | ></A |
| 413 | ></P |
| 414 | ><P |
| 415 | >There has been an attempt to systematize exit status numbers |
| 416 | (see <TT |
| 417 | CLASS="FILENAME" |
| 418 | >/usr/include/sysexits.h</TT |
| 419 | >), |
| 420 | but this is intended for C and C++ programmers. A similar |
| 421 | standard for scripting might be appropriate. The author of |
| 422 | this document proposes restricting user-defined exit codes to |
| 423 | the range <SPAN |
| 424 | CLASS="RETURNVALUE" |
| 425 | >64 - 113</SPAN |
| 426 | > (in addition to |
| 427 | <SPAN |
| 428 | CLASS="RETURNVALUE" |
| 429 | >0</SPAN |
| 430 | >, for success), to conform with |
| 431 | the C/C++ standard. This would allot 50 valid codes, and make |
| 432 | troubleshooting scripts more straightforward. |
| 433 | <A |
| 434 | NAME="AEN23647" |
| 435 | HREF="#FTN.AEN23647" |
| 436 | >[2]</A |
| 437 | > |
| 438 | All user-defined exit codes in the accompanying examples to |
| 439 | this document conform to this standard, except where overriding |
| 440 | circumstances exist, as in <A |
| 441 | HREF="variables2.html#TMDIN" |
| 442 | >Example 9-2</A |
| 443 | >.</P |
| 444 | ><DIV |
| 445 | CLASS="NOTE" |
| 446 | ><TABLE |
| 447 | CLASS="NOTE" |
| 448 | WIDTH="100%" |
| 449 | BORDER="0" |
| 450 | ><TR |
| 451 | ><TD |
| 452 | WIDTH="25" |
| 453 | ALIGN="CENTER" |
| 454 | VALIGN="TOP" |
| 455 | ><IMG |
| 456 | SRC="common/note.png" |
| 457 | HSPACE="5" |
| 458 | ALT="Note"></TD |
| 459 | ><TD |
| 460 | ALIGN="LEFT" |
| 461 | VALIGN="TOP" |
| 462 | ><P |
| 463 | >Issuing a <A |
| 464 | HREF="variables2.html#XSTATVARREF" |
| 465 | >$?</A |
| 466 | > from |
| 467 | the command-line after a shell script exits gives |
| 468 | results consistent with the table above only from the |
| 469 | Bash or <I |
| 470 | CLASS="FIRSTTERM" |
| 471 | >sh</I |
| 472 | > prompt. Running the |
| 473 | <I |
| 474 | CLASS="FIRSTTERM" |
| 475 | >C-shell</I |
| 476 | > or <I |
| 477 | CLASS="FIRSTTERM" |
| 478 | >tcsh</I |
| 479 | > |
| 480 | may give different values in some cases.</P |
| 481 | ></TD |
| 482 | ></TR |
| 483 | ></TABLE |
| 484 | ></DIV |
| 485 | ></DIV |
| 486 | ><H3 |
| 487 | CLASS="FOOTNOTES" |
| 488 | >Notes</H3 |
| 489 | ><TABLE |
| 490 | BORDER="0" |
| 491 | CLASS="FOOTNOTES" |
| 492 | WIDTH="100%" |
| 493 | ><TR |
| 494 | ><TD |
| 495 | ALIGN="LEFT" |
| 496 | VALIGN="TOP" |
| 497 | WIDTH="5%" |
| 498 | ><A |
| 499 | NAME="FTN.AEN23629" |
| 500 | HREF="exitcodes.html#AEN23629" |
| 501 | >[1]</A |
| 502 | ></TD |
| 503 | ><TD |
| 504 | ALIGN="LEFT" |
| 505 | VALIGN="TOP" |
| 506 | WIDTH="95%" |
| 507 | ><P |
| 508 | ><A |
| 509 | NAME="EXCOOR" |
| 510 | ></A |
| 511 | >Out of range exit values |
| 512 | can result in unexpected exit codes. An exit value |
| 513 | greater than <SPAN |
| 514 | CLASS="ERRORCODE" |
| 515 | >255</SPAN |
| 516 | > returns an |
| 517 | exit code <A |
| 518 | HREF="operations.html#MODULOREF" |
| 519 | >modulo</A |
| 520 | > |
| 521 | <SPAN |
| 522 | CLASS="ERRORCODE" |
| 523 | >256</SPAN |
| 524 | >. For example, <I |
| 525 | CLASS="FIRSTTERM" |
| 526 | >exit |
| 527 | 3809</I |
| 528 | > gives an exit code of <SPAN |
| 529 | CLASS="ERRORCODE" |
| 530 | >225</SPAN |
| 531 | > |
| 532 | (3809 % 256 = 225).</P |
| 533 | ></TD |
| 534 | ></TR |
| 535 | ><TR |
| 536 | ><TD |
| 537 | ALIGN="LEFT" |
| 538 | VALIGN="TOP" |
| 539 | WIDTH="5%" |
| 540 | ><A |
| 541 | NAME="FTN.AEN23647" |
| 542 | HREF="exitcodes.html#AEN23647" |
| 543 | >[2]</A |
| 544 | ></TD |
| 545 | ><TD |
| 546 | ALIGN="LEFT" |
| 547 | VALIGN="TOP" |
| 548 | WIDTH="95%" |
| 549 | ><P |
| 550 | >An update of <TT |
| 551 | CLASS="FILENAME" |
| 552 | >/usr/include/sysexits.h</TT |
| 553 | > |
| 554 | allocates previously unused exit codes from <SPAN |
| 555 | CLASS="RETURNVALUE" |
| 556 | >64 |
| 557 | - 78</SPAN |
| 558 | >. It may be anticipated that the range of |
| 559 | unallotted exit codes will be further restricted in the future. |
| 560 | The author of this document will <SPAN |
| 561 | CLASS="emphasis" |
| 562 | ><I |
| 563 | CLASS="EMPHASIS" |
| 564 | >not</I |
| 565 | ></SPAN |
| 566 | > do |
| 567 | fixups on the scripting examples to conform to the changing |
| 568 | standard. This should not cause any problems, since there |
| 569 | is no overlap or conflict in usage of exit codes between |
| 570 | compiled C/C++ binaries and shell scripts.</P |
| 571 | ></TD |
| 572 | ></TR |
| 573 | ></TABLE |
| 574 | ><DIV |
| 575 | CLASS="NAVFOOTER" |
| 576 | ><HR |
| 577 | ALIGN="LEFT" |
| 578 | WIDTH="100%"><TABLE |
| 579 | SUMMARY="Footer navigation table" |
| 580 | WIDTH="100%" |
| 581 | BORDER="0" |
| 582 | CELLPADDING="0" |
| 583 | CELLSPACING="0" |
| 584 | ><TR |
| 585 | ><TD |
| 586 | WIDTH="33%" |
| 587 | ALIGN="left" |
| 588 | VALIGN="top" |
| 589 | ><A |
| 590 | HREF="pathmanagement.html" |
| 591 | ACCESSKEY="P" |
| 592 | >Prev</A |
| 593 | ></TD |
| 594 | ><TD |
| 595 | WIDTH="34%" |
| 596 | ALIGN="center" |
| 597 | VALIGN="top" |
| 598 | ><A |
| 599 | HREF="index.html" |
| 600 | ACCESSKEY="H" |
| 601 | >Home</A |
| 602 | ></TD |
| 603 | ><TD |
| 604 | WIDTH="33%" |
| 605 | ALIGN="right" |
| 606 | VALIGN="top" |
| 607 | ><A |
| 608 | HREF="ioredirintro.html" |
| 609 | ACCESSKEY="N" |
| 610 | >Next</A |
| 611 | ></TD |
| 612 | ></TR |
| 613 | ><TR |
| 614 | ><TD |
| 615 | WIDTH="33%" |
| 616 | ALIGN="left" |
| 617 | VALIGN="top" |
| 618 | >Parsing and Managing Pathnames</TD |
| 619 | ><TD |
| 620 | WIDTH="34%" |
| 621 | ALIGN="center" |
| 622 | VALIGN="top" |
| 623 | > </TD |
| 624 | ><TD |
| 625 | WIDTH="33%" |
| 626 | ALIGN="right" |
| 627 | VALIGN="top" |
| 628 | >A Detailed Introduction to I/O and I/O Redirection</TD |
| 629 | ></TR |
| 630 | ></TABLE |
| 631 | ></DIV |
| 632 | ></BODY |
| 633 | ></HTML |
| 634 | > |