Skip to main content
Add deprecation note for `sysexits.h` (I don't have rep to comment, not sure if using edit instead is correct?)
Source Link

Additionally this has now been deprecated and its usage is discouraged (FreeBSD, OpenBSD).

Additionally this has now been deprecated and its usage is discouraged (FreeBSD, OpenBSD).

an exit code of zero usually doesn't indicate failure
Source Link
ilkkachu
  • 148.2k
  • 16
  • 268
  • 441

Exit codes indicate asuccess or failure condition when ending a program, and they fall between 0 and 255.  The shell and its builtins may use the values above 125 specially to indicate specific failure modes, so the list of codes can vary between shells and operating systems (e.g., Bash uses the value 128+N as the exit status).  See: Bash - 3.7.5 Exit Status or man bash.

Exit codes indicate a failure condition when ending a program, and they fall between 0 and 255.  The shell and its builtins may use the values above 125 specially to indicate specific failure modes, so the list of codes can vary between shells and operating systems (e.g., Bash uses the value 128+N as the exit status).  See: Bash - 3.7.5 Exit Status or man bash.

Exit codes indicate success or failure when ending a program, and they fall between 0 and 255.  The shell and its builtins may use the values above 125 specially to indicate specific failure modes, so the list of codes can vary between shells and operating systems (e.g., Bash uses the value 128+N as the exit status).  See: Bash - 3.7.5 Exit Status or man bash.

Fix broken link using Wayback Machine.  Tweaked grammar, wording, etc.
Source Link

Exit codes indicatesindicate a failure condition when ending a program and they, and they fall between 0 0 and 255 255. The  The shell and its builtins may use especially the values above 125 to indicatespecially to indicate specific failure modes, so list so the list of codes can vary between shells and operating systems    (e.g., Bash uses the value 128+N128+N as the exit status). See  See: Bash - 3.7.5 Exit Status or man bash.

In general, a zero exit status indicates that a command succeeded,   and a non-zero exit status indicates failure.

To check which error code is returned by the command, you you can print $? for the last exit code or ${PIPESTATUS[@]} which which gives a list of exit status values from pipeline (in Bash) after after a shell script exits.

There is no full list of all exit codes which can be foundfound; however, however there has been an attempt to systematize exit status numbers in kernel source, but.  But this is mainmainly intended for C/C++ programmers and similar, and a similar standard for scripting might be appropriate.

The above list allocates previously unused exit codes from 64-78. The range  The range of unallottedunallocated exit codes will be further restricted in the future.

However above, the above values are mainly used in sendmail and used by pretty much nobody else, so they aren't anything remotely close to a standard (as pointed out by @GillesGilles).

In shell, the exit status values are as followfollows (based on Bash):

  • 1-125 - Command did not complete successfully. Check  Check the command's man page for the meaning of the status, few.  A few examples below:

  • 1 - Catchall for general errors

    Miscellaneous errors, such as "divide by zero" and other impermissible operations.

    Example:

      $ let "var1 = 1/0"; echo $?
      -bash: let: var1 = 1/0: division by 0 (error token is "0")
      1
    
  • 2 - Misuse of shell builtins (according to Bash documentation)

    Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison).

    Example:

       empty_function() {}
    
  • 6 - No such device or address

    Example:

      $ curl foo; echo $?
      curl: (6) Could not resolve host: foo
      6
    
  • 124 - command times out

  • 125 - if a command itself failssee: coreutils (see: coreutils)

  • 126 - if command is found but cannot be invoked (e.g., is not executable)

    Permission problem or command is not an executable.

    Example:

      $ /dev/null
      $ /etc/hosts; echo $?
      -bash: /etc/hosts: Permission denied
      126
    
  • 127 - if a command cannot be found, the child process created to execute it returns that status

    Possible problem with $PATH or a typo.

    Example:

      $ foo; echo $?
      -bash: foo: command not found
      127
    
  • 128 - Invalid argument to exit

    exit takes only integer args in the range 0 - 255.

    Example:

      $ exit 3.14159
      -bash: exit: 3.14159: numeric argument required
    
  • 128-254 - fatal error signal "n" - command died due to receiving a signal. The signal code is added to 128 (128 + SIGNAL) to get the status (Linux: man 7 signal, BSD: man signal), few examples below:

  • 130 - command terminated due to Ctrl-C being pressed, 130-128=2 (SIGINT)

    Example:

      $ cat
      ^C
      $ echo $?
      130
    
  • 137 - if command is sent the KILL(9) signal (128+9), the exit status of command otherwise

    kill -9 $PPID of script.

  • 141 - SIGPIPE - write on a pipe with no reader

    Example:

      $ hexdump -n100000 /dev/urandom | tee &>/dev/null >(cat > file1.txt) >(cat > file2.txt) >(cat > file3.txt) >(cat > file4.txt) >(cat > file5.txt)
      $ find . -name '*.txt' -print0 | xargs -r0 cat | tee &>/dev/null >(head /dev/stdin > head.out) >(tail /dev/stdin > tail.out)
      xargs: cat: terminated by signal 13
      $ echo ${PIPESTATUS[@]}
      0 125 141
    
  • 143 - command terminated by signal code 15 (128+15=143)

    Example:

      $ sleep 5 && killall sleep &
      [1] 19891
      $ sleep 100; echo $?
      Terminated: 15
      143
    
  • 255* - exit status out of range.

    exit takes only integer args in the range 0 - 255.

    Example:

      $ sh -c 'exit 3.14159'; echo $?
      sh: line 0: exit: 3.14159: numeric argument required
      255
    

Please note that out of range exit values can result in unexpected exit codes (e.g. exit 3809, exit 3809 gives an exit code of 225, because 3809 % 256 = 225).

Exit codes indicates a failure condition when ending a program and they fall between 0 and 255. The shell and its builtins may use especially the values above 125 to indicate specific failure modes, so list of codes can vary between shells and operating systems  (e.g. Bash uses the value 128+N as the exit status). See: Bash - 3.7.5 Exit Status or man bash.

In general a zero exit status indicates that a command succeeded,  a non-zero exit status indicates failure.

To check which error code is returned by the command, you can print $? for the last exit code or ${PIPESTATUS[@]} which gives a list of exit status values from pipeline (in Bash) after a shell script exits.

There is no full list of all exit codes which can be found, however there has been an attempt to systematize exit status numbers in kernel source, but this is main intended for C/C++ programmers and similar standard for scripting might be appropriate.

The above list allocates previously unused exit codes from 64-78. The range of unallotted exit codes will be further restricted in the future.

However above values are mainly used in sendmail and used by pretty much nobody else, so they aren't anything remotely close to a standard (as pointed by @Gilles).

In shell the exit status are as follow (based on Bash):

  • 1-125 - Command did not complete successfully. Check the command's man page for the meaning of the status, few examples below:

  • 1 - Catchall for general errors

    Miscellaneous errors, such as "divide by zero" and other impermissible operations.

    Example:

      $ let "var1 = 1/0"; echo $?
      -bash: let: var1 = 1/0: division by 0 (error token is "0")
      1
    
  • 2 - Misuse of shell builtins (according to Bash documentation)

    Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison).

    Example:

       empty_function() {}
    
  • 6 - No such device or address

    Example:

      $ curl foo; echo $?
      curl: (6) Could not resolve host: foo
      6
    
  • 124 - command times out

  • 125 - if a command itself failssee: coreutils

  • 126 - if command is found but cannot be invoked (e.g. is not executable)

    Permission problem or command is not an executable.

    Example:

      $ /dev/null
      $ /etc/hosts; echo $?
      -bash: /etc/hosts: Permission denied
      126
    
  • 127 - if a command cannot be found, the child process created to execute it returns that status

    Possible problem with $PATH or a typo.

    Example:

      $ foo; echo $?
      -bash: foo: command not found
      127
    
  • 128 - Invalid argument to exit

    exit takes only integer args in the range 0 - 255.

    Example:

      $ exit 3.14159
      -bash: exit: 3.14159: numeric argument required
    
  • 128-254 - fatal error signal "n" - command died due to receiving a signal. The signal code is added to 128 (128 + SIGNAL) to get the status (Linux: man 7 signal, BSD: man signal), few examples below:

  • 130 - command terminated due to Ctrl-C being pressed, 130-128=2 (SIGINT)

    Example:

      $ cat
      ^C
      $ echo $?
      130
    
  • 137 - if command is sent the KILL(9) signal (128+9), the exit status of command otherwise

    kill -9 $PPID of script.

  • 141 - SIGPIPE - write on a pipe with no reader

    Example:

      $ hexdump -n100000 /dev/urandom | tee &>/dev/null >(cat > file1.txt) >(cat > file2.txt) >(cat > file3.txt) >(cat > file4.txt) >(cat > file5.txt)
      $ find . -name '*.txt' -print0 | xargs -r0 cat | tee &>/dev/null >(head /dev/stdin > head.out) >(tail /dev/stdin > tail.out)
      xargs: cat: terminated by signal 13
      $ echo ${PIPESTATUS[@]}
      0 125 141
    
  • 143 - command terminated by signal code 15 (128+15=143)

    Example:

      $ sleep 5 && killall sleep &
      [1] 19891
      $ sleep 100; echo $?
      Terminated: 15
      143
    
  • 255* - exit status out of range.

    exit takes only integer args in the range 0 - 255.

    Example:

      $ sh -c 'exit 3.14159'; echo $?
      sh: line 0: exit: 3.14159: numeric argument required
      255
    

Please note that out of range exit values can result in unexpected exit codes (e.g. exit 3809 gives an exit code of 225, 3809 % 256 = 225).

Exit codes indicate a failure condition when ending a program, and they fall between 0 and 255.  The shell and its builtins may use the values above 125 specially to indicate specific failure modes, so the list of codes can vary between shells and operating systems  (e.g., Bash uses the value 128+N as the exit status).  See: Bash - 3.7.5 Exit Status or man bash.

In general, a zero exit status indicates that a command succeeded, and a non-zero exit status indicates failure.

To check which error code is returned by the command, you can print $? for the last exit code or ${PIPESTATUS[@]} which gives a list of exit status values from pipeline (in Bash) after a shell script exits.

There is no full list of all exit codes which can be found; however, there has been an attempt to systematize exit status numbers in kernel source.  But this is mainly intended for C/C++ programmers, and a similar standard for scripting might be appropriate.

The above list allocates previously unused exit codes from 64-78.  The range of unallocated exit codes will be further restricted in the future.

However, the above values are mainly used in sendmail and used by pretty much nobody else, so they aren't anything remotely close to a standard (as pointed out by Gilles).

In shell, the exit status values are as follows (based on Bash):

  • 1-125 - Command did not complete successfully.  Check the command's man page for the meaning of the status.  A few examples below:

  • 1 - Catchall for general errors

    Miscellaneous errors, such as "divide by zero" and other impermissible operations.

    Example:

      $ let "var1 = 1/0"; echo $?
      -bash: let: var1 = 1/0: division by 0 (error token is "0")
      1
    
  • 2 - Misuse of shell builtins (according to Bash documentation)

    Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison).

    Example:

       empty_function() {}
    
  • 6 - No such device or address

    Example:

      $ curl foo; echo $?
      curl: (6) Could not resolve host: foo
      6
    
  • 124 - command times out

  • 125 - if a command itself fails (see: coreutils)

  • 126 - if command is found but cannot be invoked (e.g., is not executable)

    Permission problem or command is not an executable.

    Example:

      $ /dev/null
      $ /etc/hosts; echo $?
      -bash: /etc/hosts: Permission denied
      126
    
  • 127 - if a command cannot be found, the child process created to execute it returns that status

    Possible problem with $PATH or a typo.

    Example:

      $ foo; echo $?
      -bash: foo: command not found
      127
    
  • 128 - Invalid argument to exit

    exit takes only integer args in the range 0 - 255.

    Example:

      $ exit 3.14159
      -bash: exit: 3.14159: numeric argument required
    
  • 128-254 - fatal error signal "n" - command died due to receiving a signal. The signal code is added to 128 (128 + SIGNAL) to get the status (Linux: man 7 signal, BSD: man signal), few examples below:

  • 130 - command terminated due to Ctrl-C being pressed, 130-128=2 (SIGINT)

    Example:

      $ cat
      ^C
      $ echo $?
      130
    
  • 137 - if command is sent the KILL(9) signal (128+9), the exit status of command otherwise

    kill -9 $PPID of script.

  • 141 - SIGPIPE - write on a pipe with no reader

    Example:

      $ hexdump -n100000 /dev/urandom | tee &>/dev/null >(cat > file1.txt) >(cat > file2.txt) >(cat > file3.txt) >(cat > file4.txt) >(cat > file5.txt)
      $ find . -name '*.txt' -print0 | xargs -r0 cat | tee &>/dev/null >(head /dev/stdin > head.out) >(tail /dev/stdin > tail.out)
      xargs: cat: terminated by signal 13
      $ echo ${PIPESTATUS[@]}
      0 125 141
    
  • 143 - command terminated by signal code 15 (128+15=143)

    Example:

      $ sleep 5 && killall sleep &
      [1] 19891
      $ sleep 100; echo $?
      Terminated: 15
      143
    
  • 255* - exit status out of range.

    exit takes only integer args in the range 0 - 255.

    Example:

      $ sh -c 'exit 3.14159'; echo $?
      sh: line 0: exit: 3.14159: numeric argument required
      255
    

Please note that out of range exit values can result in unexpected exit codes (e.g., exit 3809 gives an exit code of 225, because 3809 % 256 = 225).

replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link
Loading
Clarification as per @Gilles comment.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Removes errno values which are used by system APIs as per comment.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Expanding post a bit.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Expanding post a bit.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading