Recall & Review
beginner
What does
exit N do in a bash script?exit N stops the script and sends the number N as the exit code to the system. This code tells if the script ended successfully or with an error.
Click to reveal answer
beginner
Why use custom exit codes instead of just
exit 0 or exit 1?<p>Custom exit codes let you show different reasons for failure or success. For example, <code>exit 2</code> might mean "file not found," while <code>exit 3</code> means "permission denied." This helps other scripts or users understand what happened.</p>Click to reveal answer
beginner
What is the valid range for exit codes in bash scripts?
Exit codes must be between 0 and 255. 0 means success, and any number from 1 to 255 means different errors or statuses.
Click to reveal answer
beginner
How can you check the exit code of the last command in bash?
Use the special variable $?. It holds the exit code of the last command or script run.
Click to reveal answer
beginner
Example: What exit code will this script return?<br>
#!/bin/bash if [ ! -f myfile.txt ]; then exit 2 else exit 0 fi
If myfile.txt does not exist, the script exits with code 2. Otherwise, it exits with 0 meaning success.
Click to reveal answer
What does
exit 0 usually mean in a bash script?✗ Incorrect
exit 0 means the script finished without errors.
Which exit code range is valid in bash scripts?
✗ Incorrect
Bash exit codes must be between 0 and 255.
How do you get the exit code of the last command in bash?
✗ Incorrect
$? holds the exit code of the last command.
If a script ends with
exit 3, what does it mean?✗ Incorrect
Exit codes other than 0 usually mean different errors or statuses.
What happens if you use
exit 300 in a bash script?✗ Incorrect
Exit codes wrap around modulo 256, so 300 becomes 44.
Explain how and why you would use custom exit codes in a bash script.
Think about how scripts tell the system if they succeeded or failed.
You got /4 concepts.
Describe how to check the exit code of the last command and why it is useful.
This is how you know if a command worked before moving on.
You got /4 concepts.