๐งพ Commands and Exit Codes
Every command returns an exit code that indicates success or failure. Understanding these codes is critical for scripting robust workflows.
๐ข Exit Code Meaning
Exit codes range from 0 to 255. By convention:
- 0 = Success
- 1โ255 = Various types of failures
Check last commandโs exit code with $?:
1 2 3 4 | |
๐งช Testing Exit Codes in Scripts
Use conditional statements to check results:
1 2 3 4 5 6 7 | |
Or more concisely:
1 2 3 4 5 | |
๐งฐ Common Exit Codes
| Code | Meaning | Examples |
|---|---|---|
0 |
Success | Normal completion |
1 |
General error | Misuse of shell builtins |
2 |
Misuse of shell builtin | Incorrect usage |
126 |
Command found but not executable | Permission denied |
127 |
Command not found | Typo or missing PATH entry |
130 |
Script terminated by Ctrl+C | Signal SIGINT received |
Full list available in
sysexits.horman sysexits.
๐งช Practical Example
Hereโs how to handle errors gracefully:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
This ensures failures don't get ignored silently.
๐ Continue to: Path and Executables