Exit Codes Reference

Exit codes reference — what 0, 1, 2, 126, 127, 128+N mean. $? to check last command. SIGKILL=137, SIGSEGV=139. Every shell exit code explained with causes.

12 min read

What it is

A reference for common exit codes used by shell commands, helping you understand why a command failed.

Installation

This is a conceptual reference. No installation required.

Core Concepts

  • Exit Code: A small integer returned by a command upon completion.
    • 0: Indicates successful execution.
    • Non-zero: Indicates an error or abnormal termination. The specific value often provides a clue about the nature of the error.

Commands / Usage

This section describes common exit codes and their typical meanings.


General Success

  • 0
    • Meaning: Command executed successfully.

General Errors

  • 1
    • Meaning: Generic error code, often used when no more specific error code is available.
    • Example: grep "nonexistent_pattern" file.txt might return 1 if the pattern is not found (though grep has specific codes for this).

Command-Specific Errors

  • 2

    • Meaning: Misuse of shell builtins (e.g., exit, return).
    • Example: exit 2
  • 126

    • Meaning: Command invoked cannot execute (e.g., permission denied).
    • Example: chmod 000 my_script.sh && ./my_script.sh
  • 127

    • Meaning: Command not found. Often due to a typo in the command name or it not being in the system’s PATH.
    • Example: lsx (if lsx is not a command)
  • 128

    • Meaning: Indicates an error in the exit status of the command itself. The actual exit code is 128 + n, where n is the signal number that terminated the process.
    • Example: If a process is terminated by signal 9 (SIGKILL), its exit code would be 128 + 9 = 137.
  • 130

    • Meaning: Command terminated by Ctrl+C (SIGINT).
    • Example: Running sleep 60 and pressing Ctrl+C.

Errors from Specific Commands (Common Examples)

  • grep

    • 0: One or more lines were selected.
    • 1: No lines were selected.
    • 2: An error occurred.
    • Example: grep "pattern" file.txt returns 0 if found, 1 if not found.
  • find

    • 0: Successful execution.
    • Non-zero: Errors occurred during traversal (e.g., permission denied on directories).
    • Example: find /root -name "*.txt" might return non-zero if it can’t access subdirectories of /root.
  • ssh

    • 0: Connection successful, command executed.
    • 255: General SSH error (authentication failure, connection refused, etc.).
    • Example: ssh user@nonexistent-host will likely return 255.
  • curl

    • 0: Success.
    • 6: Couldn’t resolve host.
    • 7: Failed to connect to host.
    • 22: HTTP page not retrieved (e.g., 404 Not Found).
    • 35: SSL connect error.
    • Example: curl http://localhost:9999 (if nothing is listening) might return 7. curl http://example.com/nonexistent-page might return 22.
  • rsync

    • 0: Success.
    • 1: Syntax or usage error.
    • 2: Protocol incompatibility.
    • 3: Errors selecting input/output files, dirs.
    • 4: Requested action not supported.
    • 5: Error starting client-server protocol.
    • 10: Error starting client-server protocol.
    • 11: Error in socket I/O.
    • 12: Error in file I/O.
    • 13: Error data stream.
    • 14: Errors in running rsync process.
    • 20: Error in section negotiation.
    • 21: No files found from the input.
    • 22: Error in --exclude / --include processing.
    • 23: Error in remote I/O.
    • 24: Error in --files-from generation.
    • 25: Bad exit status from rsync wrapper.
    • 30: Partial transfer due to error.
    • 35: This rsync is older than the remote rsync.
    • 45: The --local option was used together with a remote or local file name.
    • 50: Local I/O error.
    • 51: Remote path does not exist or does not created.
    • 52: Error generating temporary file name.
    • 53: Too many files to create temporary file.
    • 54: Remote file creation needs a parent directory.
    • 55: Remote directory creation needs a parent directory.
    • 56: Requested --files-from format not supported.
    • 57: Cannot stat source.
    • 58: Source not found.
    • 59: Invalid argument to --max-delete.
    • 60: Remote message was too long.
    • 61: Remote message was invalid.
    • 62: Remote message was empty.
    • 63: Checksum mismatch.
    • 64: Object not found.
    • 65: Chunk checksum mismatch.
    • 66: Object has been deleted.
    • 67: Invalid object name.
    • 68: Invalid object size.
    • 69: Invalid object modification time.
    • 70: Invalid object permissions.
    • 71: Invalid object owner/group.
    • 72: Invalid object symlink.
    • 73: Object type mismatch.
    • 74: Object name too long.
    • 75: Object name contains invalid characters.
    • 76: Object name is invalid.
    • 77: Object name is reserved.
    • 78: Object name is a directory.
    • 79: Object name is a file.
    • 80: Object name is a symlink.
    • 81: Object name is a device.
    • 82: Object name is a socket.
    • 83: Object name is a pipe.
    • 84: Object name is a FIFO.
    • 85: Object name is a hardlink.
    • 86: Object name is a special file.
    • 87: Object name is a character device.
    • 88: Object name is a block device.
    • 89: Object name is a remote file.
    • 90: Object name is a remote directory.
    • 91: Object name is a remote symlink.
    • 92: Object name is a remote device.
    • 93: Object name is a remote socket.
    • 94: Object name is a remote pipe.
    • 95: Object name is a remote FIFO.
    • 96: Object name is a remote hardlink.
    • 97: Object name is a remote special file.
    • 98: Object name is a remote character device.
    • 99: Object name is a remote block device.
    • 100: Object name is a remote object.
    • 101: Object name is a remote object.
    • 102: Object name is a remote object.
    • 103: Object name is a remote object.
    • 104: Object name is a remote object.
    • 105: Object name is a remote object.
    • 106: Object name is a remote object.
    • 107: Object name is a remote object.
    • 108: Object name is a remote object.
    • 109: Object name is a remote object.
    • 110: Object name is a remote object.
    • 111: Object name is a remote object.
    • 112: Object name is a remote object.
    • 113: Object name is a remote object.
    • 114: Object name is a remote object.
    • 115: Object name is a remote object.
    • 116: Object name is a remote object.
    • 117: Object name is a remote object.
    • 118: Object name is a remote object.
    • 119: Object name is a remote object.
    • 120: Object name is a remote object.
    • 121: Object name is a remote object.
    • 122: Object name is a remote object.
    • 123: Object name is a remote object.
    • 124: Object name is a remote object.
    • 125: Object name is a remote object.
    • 126: Object name is a remote object.
    • 127: Object name is a remote object.
    • 128: Object name is a remote object.
    • 129: Object name is a remote object.
    • 130: Object name is a remote object.
    • 131: Object name is a remote object.
    • 132: Object name is a remote object.
    • 133: Object name is a remote object.
    • 134: Object name is a remote object.
    • 135: Object name is a remote object.
    • 136: Object name is a remote object.
    • 137: Object name is a remote object.
    • 138: Object name is a remote object.
    • 139: Object name is a remote object.
    • 140: Object name is a remote object.
    • 141: Object name is a remote object.
    • 142: Object name is a remote object.
    • 143: Object name is a remote object.
    • 144: Object name is a remote object.
    • 145: Object name is a remote object.
    • 146: Object name is a remote object.
    • 147: Object name is a remote object.
    • 148: Object name is a remote object.
    • 149: Object name is a remote object.
    • 150: Object name is a remote object.
    • 151: Object name is a remote object.
    • 152: Object name is a remote object.
    • 153: Object name is a remote object.
    • 154: Object name is a remote object.
    • 155: Object name is a remote object.
    • 156: Object name is a remote object.
    • 157: Object name is a remote object.
    • 158: Object name is a remote object.
    • 159: Object name is a remote object.
    • 160: Object name is a remote object.
    • 161: Object name is a remote object.
    • 162: Object name is a remote object.
    • 163: Object name is a remote object.
    • 164: Object name is a remote object.
    • 165: Object name is a remote object.
    • 166: Object name is a remote object.
    • 167: Object name is a remote object.
    • 168: Object name is a remote object.
    • 169: Object name is a remote object.
    • 170: Object name is a remote object.
    • 171: Object name is a remote object.
    • 172: Object name is a remote object.
    • 173: Object name is a remote object.
    • 174: Object name is a remote object.
    • 175: Object name is a remote object.
    • 176: Object name is a remote object.
    • 177: Object name is a remote object.
    • 178: Object name is a remote object.
    • 179: Object name is a remote object.
    • 180: Object name is a remote object.
    • 181: Object name is a remote object.
    • 182: Object name is a remote object.
    • 183: Object name is a remote object.
    • 184: Object name is a remote object.
    • 185: Object name is a remote object.
    • 186: Object name is a remote object.
    • 187: Object name is a remote object.
    • 188: Object name is a remote object.
    • 189: Object name is a remote object.
    • 190: Object name is a remote object.
    • 191: Object name is a remote object.
    • 192: Object name is a remote object.
    • 193: Object name is a remote object.
    • 194: Object name is a remote object.
    • 195: Object name is a remote object.
    • 196: Object name is a remote object.
    • 197: Object name is a remote object.
    • 198: Object name is a remote object.
    • 199: Object name is a remote object.
    • 200: Object name is a remote object.
    • 201: Object name is a remote object.
    • 202: Object name is a remote object.
    • 203: Object name is a remote object.
    • 204: Object name is a remote object.
    • 205: Object name is a remote object.
    • 206: Object name is a remote object.
    • 207: Object name is a remote object.
    • 208: Object name is a remote object.
    • 209: Object name is a remote object.
    • 210: Object name is a remote object.
    • 211: Object name is a remote object.
    • 212: Object name is a remote object.
    • 213: Object name is a remote object.
    • 214: Object name is a remote object.
    • 215: Object name is a remote object.
    • 216: Object name is a remote object.
    • 217: Object name is a remote object.
    • 218: Object name is a remote object.
    • 219: Object name is a remote object.
    • 220: Object name is a remote object.
    • 221: Object name is a remote object.
    • 222: Object name is a remote object.
    • 223: Object name is a remote object.
    • 224: Object name is a remote object.
    • 225: Object name is a remote object.
    • 226: Object name is a remote object.
    • 227: Object name is a remote object.
    • 228: Object name is a remote object.
    • 229: Object name is a remote object.
    • 230: Object name is a remote object.
    • 231: Object name is a remote object.
    • 232: Object name is a remote object.
    • 233: Object name is a remote object.
    • 234: Object name is a remote object.
    • 235: Object name is a remote object.
    • 236: Object name is a remote object.
    • 237: Object name is a remote object.
    • 238: Object name is a remote object.
    • 239: Object name is a remote object.
    • 240: Object name is a remote object.
    • 241: Object name is a remote object.
    • 242: Object name is a remote object.
    • 243: Object name is a remote object.
    • 244: Object name is a remote object.
    • 245: Object name is a remote object.
    • 246: Object name is a remote object.
    • 247: Object name is a remote object.
    • 248: Object name is a remote object.
    • 249: Object name is a remote object.
    • 250: Object name is a remote object.
    • 251: Object name is a remote object.
    • 252: Object name is a remote object.
    • 253: Object name is a remote object.
    • 254: Object name is a remote object.
    • 255: Unknown remote error.
    • Example: rsync -avz /local/dir/ user@remote:/remote/dir/ might return 23 if there are I/O errors on the destination.

Common Patterns

  • Checking for success:

    command_to_run
    if [ $? -eq 0 ]; then
        echo "Command succeeded."
    else
        echo "Command failed with exit code $?."
    fi
    
    • Explanation: This checks the special shell variable $? which holds the exit code of the most recently executed foreground command.
  • Ignoring errors:

    command_to_run || echo "Command failed, but I'm ignoring it."
    
    • Explanation: The || operator executes the command on the right only if the command on the left fails (returns a non-zero exit code).
  • Executing a command only if the previous one fails:

    command_that_might_fail
    if [ $? -ne 0 ]; then
        echo "It failed! Running cleanup..."
        cleanup_command
    fi
    
    • Explanation: This pattern explicitly checks if the exit code is not zero (-ne) and then executes a recovery or cleanup action.
  • Using set -e to exit on error:

    #!/bin/bash
    set -e
    echo "This will run."
    ls /nonexistent_directory # This will cause the script to exit immediately
    echo "This will NOT run."
    
    • Explanation: set -e (or set -o errexit) makes the script exit immediately if any command fails (returns a non-zero exit code). This is a common way to ensure scripts don’t continue after an error.
  • Using set -o pipefail:

    #!/bin/bash
    set -e
    set -o pipefail
    
    # This pipeline will exit with code 1 (from grep) because of pipefail
    ls / | grep "nonexistent_pattern"
    echo "This line will not be reached."
    
    • Explanation: By default, if a pipeline has multiple commands, the exit code of the pipeline is the exit code of the last command. set -o pipefail makes the pipeline’s exit code the exit code of the first command in the pipeline that failed. Combined with set -e, this ensures scripts exit on any failure within a pipeline.

Gotchas

  • Exit code 1 is not always a critical failure: Commands like grep often return 1 when a pattern is not found, which is expected behavior, not necessarily an error. Always consult the specific command’s documentation.
  • Signals vs. Exit Codes: Exit codes in the 128 + n range indicate termination by a signal. For example, 137 means the process was killed by signal 9 (SIGKILL). This is different from a command returning a specific error code like 1 or 2.
  • Subshells and $?: The $? variable only reflects the exit code of the most recently executed foreground command. If you run commands in subshells (e.g., within (...)) or background processes, $? might not behave as expected for those specific commands.
  • set -e and || / &&: set -e can be overridden by explicit || or && constructs. A command that would normally cause set -e to exit might not if it’s part of an if condition or a logical OR/AND chain.
    set -e
    ls /nonexistent_directory || echo "Ignoring the error as requested." # Script continues
    
  • Exit code 127 is common for PATH issues: If you get a "command not found" error (exit code 127), double-check your PATH environment variable and ensure the command is installed and accessible.