shell-mastery — Exit Codes

Exit Codes

bash / zsh
ls /tmp; echo $?          # 0 = success
ls /nonexistent; echo $?  # 2 = no such file

# Capture and test
some_command
rc=$?
[[ $rc -eq 0 ]] && echo "OK" || echo "FAILED (exit $rc)"

# Inline conditional
command && echo "success" || echo "failed"
Common exit codes
0   — success
1   — general error
2   — misuse of shell builtin / no such file
126 — command found but not executable
127 — command not found
128+N — killed by signal N (e.g., 130 = Ctrl+C = SIGINT)
PowerShell
$LASTEXITCODE    # exit code of last external command (ping, curl, etc.)
$?               # True/False for last PowerShell cmdlet

ping 127.0.0.1 -n 1; $LASTEXITCODE     # 0
ping 999.999.999.999 -n 1; $LASTEXITCODE  # 1

Get-Service sshd; $?    # True if cmdlet succeeded