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
✗ Incorrect
$? stores the exit code of the last command run.
Which exit code means success?
A0
BAny non-zero number
C255
D1
✗ Incorrect
Exit code 0 means the command succeeded.
If a command fails, what is the usual exit code?
A0
B1 or other non-zero
C100
DNo exit code
✗ Incorrect
Non-zero exit codes indicate failure or errors.
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
✗ Incorrect
You check $? immediately after a command to know its result.
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
✗ Incorrect
Scripts use exit codes to decide what to do next.
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.
Practice
(1/5)
1. What does the exit code 0 usually mean in bash scripting?
easy
A. The command is still running
B. The command failed
C. The command was skipped
D. The command was successful
Solution
Step 1: Understand exit codes meaning
In bash, exit code 0 means success, any other number means failure or error.
Step 2: Interpret exit code 0
Since 0 means success, the command completed without errors.
Final Answer:
The command was successful -> Option D
Quick Check:
Exit code 0 = success [OK]
Hint: Exit code 0 means success, anything else means error [OK]
Common Mistakes:
Thinking 0 means failure
Confusing exit code with process ID
Checking $? too late after command
2. Which of the following is the correct way to check the exit code of the last command in bash?
easy
A. echo $exit_code
B. echo $?
C. echo $lastcode
D. echo $status
Solution
Step 1: Recall special variable for exit code
The special variable $? holds the exit code of the last command.
Step 2: Verify correct syntax
Using echo $? prints the exit code correctly.
Final Answer:
echo $? -> Option B
Quick Check:
Use $? to get last exit code [OK]
Hint: Use $? immediately after command to get exit code [OK]
Common Mistakes:
Using undefined variables like $exit_code
Forgetting to check $? right after command
Using $status which is not standard
3. What will be the output of this script?
ls /nonexistent_dir
echo $?
medium
A. 2
B. 0
C. No output
D. ls: cannot access '/nonexistent_dir': No such file or directory
Solution
Step 1: Understand the command behavior
The command ls /nonexistent_dir tries to list a folder that does not exist, so it fails.
Step 2: Check exit code after failure
After failure, $? holds a non-zero exit code, 2 for ls missing directories.
Final Answer:
2 -> Option A
Quick Check:
Failed command exit code ≠ 0 [OK]
Hint: Failed commands return non-zero exit codes like 2 [OK]
Common Mistakes:
Assuming output is 0 for failure
Confusing error message with exit code
Expecting no output from echo
4. Consider this script snippet:
mkdir test_dir
false
if [ $? -eq 0 ]; then
echo "Created"
else
echo "Failed"
fi
What is the problem if the script always prints "Failed" even when the directory is created?
medium
A. mkdir command does not set exit code
B. The syntax of if statement is wrong
C. The $? is checked too late after mkdir
D. echo command overwrites the exit code
Solution
Step 1: Understand exit code timing
The exit code $? must be checked immediately after the command it relates to.
Step 2: Identify delay in checking exit code
If any command runs between mkdir and checking $?, the exit code changes.
Final Answer:
The $? is checked too late after mkdir -> Option C
Quick Check:
Check $? immediately after command [OK]
Hint: Check $? right after command, no commands in between [OK]
Common Mistakes:
Checking $? after other commands
Assuming mkdir never fails
Misunderstanding if syntax
5. You want to run two commands in a script: cmd1 and cmd2. You want to run cmd2 only if cmd1 succeeded (exit code 0). Which is the best way to do this?