diff --git a/Linux/Bash Script/3-Exit-Codes.md b/Linux/Bash Script/3-Exit-Codes.md new file mode 100644 index 0000000..0f004e6 --- /dev/null +++ b/Linux/Bash Script/3-Exit-Codes.md @@ -0,0 +1,95 @@ +# ๐Ÿงพ Linux Exit Codes Cheat Sheet + +Every command in Linux returns an **exit status code** upon completion. You can check this code using: + +```bash +command +echo $? +``` + +These codes are helpful for debugging scripts or understanding command behavior. + +--- + +## โœ… Common Exit Codes & Their Meanings + +| Exit Code | Meaning | +| --------- | ----------------------------------------------- | +| `0` | โœ… Success | +| `1` | โŒ General Error | +| `2` | โš ๏ธ Misuse of Shell Built-in (bad options, etc.) | +| `126` | ๐Ÿšซ Command Found but Cannot Execute | +| `127` | โ“ Command Not Found | +| `130` | โŒจ๏ธ Terminated by `Ctrl+C` | +| `137` | ๐Ÿ’€ Killed (e.g., `kill -9`) | + +--- + +## ๐Ÿ” Exit Code Examples + +### `0` โ€” Success + +```bash +echo "Hello" +echo $? # โžœ 0 +``` + +--- + +### `1` โ€” General Error + +```bash +grep "text" non_existing_file.txt +echo $? # โžœ 1 +``` + +--- + +### `2` โ€” Misuse of Shell Built-in + +```bash +ls --wrongoption +echo $? # โžœ 2 +``` + +--- + +### `126` โ€” Command Found but Not Executable + +```bash +touch myscript.sh +chmod -x myscript.sh +./myscript.sh +echo $? # โžœ 126 +``` + +--- + +### `127` โ€” Command Not Found + +```bash +nonexistentcommand +echo $? # โžœ 127 +``` + +--- + +### `130` โ€” Script Terminated by `Ctrl+C` + +```bash +sleep 10 +# Press Ctrl+C while it sleeps +echo $? # โžœ 130 +``` + +--- + +### `137` โ€” Killed by `kill -9` + +```bash +sleep 100 & +kill -9 $! +wait +echo $? # โžœ 137 +``` +