What a shell script is in Bash Scripting - Time & Space Complexity
We want to understand how the time a shell script takes grows as it runs commands.
How does the number of commands affect the total time?
Analyze the time complexity of the following shell script.
#!/bin/bash
for file in *.txt; do
echo "Processing $file"
wc -l "$file"
done
This script lists all text files and counts lines in each one.
Look for loops or repeated commands.
- Primary operation: The for-loop that runs commands for each text file.
- How many times: Once for each .txt file in the folder.
As the number of text files grows, the script runs more commands.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times the commands inside the loop |
| 100 | About 100 times the commands inside the loop |
| 1000 | About 1000 times the commands inside the loop |
Pattern observation: The total work grows directly with the number of files.
Time Complexity: O(n)
This means the time grows in a straight line as the number of files increases.
[X] Wrong: "The script runs in the same time no matter how many files there are."
[OK] Correct: Each file adds more commands to run, so more files mean more time.
Understanding how loops affect script time helps you write efficient automation and explain your code clearly.
"What if we added a nested loop inside the for-loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a shell script is
A shell script is a text file with commands that the shell can run automatically.Step 2: Compare options to definition
Only A file containing commands that run automatically in the shell describes a file with commands running automatically in the shell.Final Answer:
A file containing commands that run automatically in the shell -> Option AQuick Check:
Shell script = file with commands [OK]
- Confusing shell script with software programs
- Thinking shell scripts are hardware
- Believing shell scripts are graphical tools
Solution
Step 1: Identify the shell script shebang
The first line in a shell script usually starts with#!/bin/bashto tell the system to use bash shell.Step 2: Check other options
<!DOCTYPE html> is for HTML, print('Hello World') is Python, function main() {} is like JavaScript. None are shell script shebangs.Final Answer:
#!/bin/bash -> Option BQuick Check:
Shell scripts start with #!/bin/bash [OK]
- Using code from other languages as shell script start
- Omitting the shebang line
- Confusing shell script with HTML or Python
#!/bin/bash echo "Hello, World!"
Solution
Step 1: Understand the echo command
Theechocommand prints the text inside quotes to the screen.Step 2: Predict the output
The script will print exactly: Hello, World!Final Answer:
Hello, World! -> Option DQuick Check:
echo prints text = Hello, World! [OK]
- Thinking echo prints the command itself
- Expecting error without cause
- Assuming no output without running script
#!/bin/bash echo Done
What is the likely problem?
Solution
Step 1: Check script permissions
If the script shows an error when run, it might not have execute permission set.Step 2: Verify other options
Echo is correct, shebang is correct, and semicolon is not needed after echo.Final Answer:
The script file is missing execute permission -> Option CQuick Check:
Missing execute permission causes errors [OK]
- Assuming syntax error without checking permissions
- Adding unnecessary semicolons
- Changing correct shebang line
Solution
Step 1: Understand automation goal
To automate daily cleaning, you need a script that runs commands automatically.Step 2: Use scheduling tool
Cron is a tool that runs scripts on a schedule, perfect for daily tasks.Final Answer:
Write a shell script with commands to delete temp files, then schedule it with cron -> Option AQuick Check:
Script + cron = daily automation [OK]
- Thinking manual typing is automation
- Ignoring scheduling tools like cron
- Running script only once instead of daily
Ready to prove it?
Pass the test and this pattern moves unknown
