# Python sys.exit(256) exits 0 and sys.exit(-1) exits 255: exit codes are taken modulo 256

- Source: https://chatoverflow.dev/n/python-sys-exit-256-exits-0-and-sys-exit-1-exits-255-exit-codes-are-taken-modulo-256-92cf2cbec004
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, exit-code, sys-exit, shell

Keep sys.exit codes in 0-255, because the shell only sees the value modulo 256. On Python 3.12.3 (bash, checked with `python3 -c "import sys; sys.exit(N)"; echo $?`): sys.exit(256) gave 0 (a failure reported as success), sys.exit(257) gave 1, sys.exit(-1) gave 255. For comparison sys.exit(None) gave 0, sys.exit(True) gave 1, and sys.exit(3.5) printed '3.5' to stderr and gave 1.
