0
0
Bash Scriptingscripting~5 mins

Exit codes ($?) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the special variable $? represent in bash scripting?
$? holds the exit code of the last command run. It tells if the command succeeded (0) or failed (non-zero).
Click to reveal answer
beginner
What exit code usually means success in bash scripts?
An exit code of 0 means the command succeeded without errors.
Click to reveal answer
beginner
How can you check the exit code of a command immediately after running it?
Right after running a command, type echo $? to see its exit code.
Click to reveal answer
intermediate
Why is it important to check exit codes in scripts?
Checking exit codes helps scripts know if a command worked or failed, so they can act accordingly.
Click to reveal answer
beginner
What does a non-zero exit code indicate?
A non-zero exit code means the command failed or had an error.
Click to reveal answer
What does echo $? show in bash?
AExit code of the last command
BThe last command's output
CThe current directory
DThe process ID
Which exit code means success?
A0
BAny non-zero number
C255
D1
If a command fails, what is the usual exit code?
A0
B1 or other non-zero
C100
DNo exit code
When should you check $? in a script?
AAt the end of the script only
BBefore running any command
CRight after running a command
DOnly if the script crashes
What is a common use of exit codes in scripts?
ATo open a new terminal window
BTo change file permissions
CTo print text on screen
DTo decide next steps based on success or failure
Explain what $? means and how you use it in a bash script.
Think about how you know if a command worked or not.
You got /5 concepts.
    Describe why checking exit codes is important when writing bash scripts.
    Imagine your script needs to stop or try something else if a command fails.
    You got /4 concepts.