Understanding Exit Codes ($?) in Bash Scripts
📖 Scenario: You are writing a simple bash script to run commands and check if they worked correctly. Exit codes help you know if a command succeeded or failed.In real life, this is like checking if a light bulb is working after you install it. If it works, you get a green light (exit code 0). If it doesn't, you get a red light (non-zero exit code).
🎯 Goal: Build a bash script that runs a command, saves its exit code using $?, and prints the exit code to understand if the command succeeded or failed.
📋 What You'll Learn
Create a variable to run a command
Save the exit code of the command using
$?Print the exit code to the terminal
💡 Why This Matters
🌍 Real World
Exit codes are used in scripts to detect errors and decide what to do next, like retrying a command or stopping the script.
💼 Career
Understanding exit codes is essential for writing reliable automation scripts and troubleshooting problems in system administration and DevOps roles.
Progress0 / 4 steps