Exit codes ($?) in Bash Scripting - Time & Space Complexity
We want to understand how checking exit codes affects the time a script takes to run.
Specifically, how does the number of commands impact the time spent checking their exit codes?
Analyze the time complexity of the following code snippet.
for file in *.txt; do
cp "$file" /backup/
if [ $? -ne 0 ]; then
echo "Copy failed for $file"
fi
done
This script copies all .txt files to a backup folder and checks the exit code after each copy to see if it succeeded.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that goes through each .txt file. - How many times: Once for each .txt file in the folder.
- The exit code check
[ $? -ne 0 ]runs once per file, right after the copy command.
As the number of .txt files grows, the script runs the copy and exit code check for each file.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copies and 10 exit code checks |
| 100 | About 100 copies and 100 exit code checks |
| 1000 | About 1000 copies and 1000 exit code checks |
Pattern observation: The total work grows directly with the number of files. Double the files, double the work.
Time Complexity: O(n)
This means the time to run grows linearly with the number of files processed.
[X] Wrong: "Checking the exit code is a separate step that doesn't add to the time complexity."
[OK] Correct: The exit code check runs every time inside the loop, so it adds work proportional to the number of files, affecting total time.
Understanding how simple checks like exit codes add to script runtime helps you write efficient scripts and explain your reasoning clearly in interviews.
What if we removed the exit code check inside the loop? How would the time complexity change?
Practice
0 usually mean in bash scripting?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 DQuick Check:
Exit code 0 = success [OK]
- Thinking 0 means failure
- Confusing exit code with process ID
- Checking $? too late after command
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
Usingecho $?prints the exit code correctly.Final Answer:
echo $? -> Option BQuick Check:
Use $? to get last exit code [OK]
- Using undefined variables like $exit_code
- Forgetting to check $? right after command
- Using $status which is not standard
ls /nonexistent_dir echo $?
Solution
Step 1: Understand the command behavior
The commandls /nonexistent_dirtries 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 AQuick Check:
Failed command exit code ≠ 0 [OK]
- Assuming output is 0 for failure
- Confusing error message with exit code
- Expecting no output from echo
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?
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 betweenmkdirand checking$?, the exit code changes.Final Answer:
The $? is checked too late after mkdir -> Option CQuick Check:
Check $? immediately after command [OK]
- Checking $? after other commands
- Assuming mkdir never fails
- Misunderstanding if syntax
cmd1 and cmd2. You want to run cmd2 only if cmd1 succeeded (exit code 0). Which is the best way to do this?Solution
Step 1: Understand conditional execution operators
The operator&&runs the second command only if the first succeeds (exit code 0).Step 2: Compare options for best practice
cmd1 && cmd2 usescmd1 && cmd2which is concise and reliable for this purpose.Final Answer:
cmd1 && cmd2 -> Option AQuick Check:
Use && to run second command only if first succeeds [OK]
- Using || which runs second if first fails
- Checking $? manually when && is simpler
- Using if with command but less concise
Ready to prove it?
Pass the test and this pattern moves unknown
