Running scripts in Bash Scripting - Time & Space Complexity
When we run a script, we want to know how long it takes as the work grows.
We ask: How does running time change when the script handles more data or commands?
Analyze the time complexity of the following code snippet.
#!/bin/bash
for file in /path/to/folder/*; do
echo "Processing $file"
grep "pattern" "$file"
done
This script lists files in a folder and searches each file for a pattern.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each file in the folder.
- How many times: Once for each file found in the folder.
As the number of files grows, the script runs the loop more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times running grep |
| 100 | 100 times running grep |
| 1000 | 1000 times running grep |
Pattern observation: The work grows directly with the number of files.
Time Complexity: O(n)
This means the running time grows in a straight line as the number of files increases.
[X] Wrong: "Running the script takes the same time no matter how many files there are."
[OK] Correct: Each file adds more work because the script processes them one by one.
Understanding how script running time grows helps you explain your code's efficiency clearly and confidently.
"What if we changed the script to process files in parallel? How would the time complexity change?"
Practice
myscript.sh after making it executable?Solution
Step 1: Understand script execution permission
Before running a script with./myscript.sh, it must have execute permission set usingchmod +x myscript.sh.Step 2: Run the script using relative path
Using./myscript.shruns the script in the current directory. Other commands likerun,execute, orstartare not valid bash commands.Final Answer:
Use./myscript.shto run the script. -> Option AQuick Check:
Executable script run with./script.sh= A [OK]
- Trying to run script without execute permission
- Using invalid commands like run or start
- Not specifying path to script
backup.sh without changing its permissions?Solution
Step 1: Understand running scripts without execute permission
If the script is not executable, you can run it by explicitly calling the shell interpreter likebash backup.sh.Step 2: Identify correct syntax
bash backup.shruns the script using bash without needing execute permission. Other options either require execute permission or are invalid commands.Final Answer:
bash backup.sh-> Option CQuick Check:
Run script with interpreter command = B [OK]
- Trying to run with ./ without execute permission
- Using invalid commands like execute
- Adding extra words like 'run' before script name
hello.sh with content:#!/bin/bash echo "Hello, $1!"
What is the output of
./hello.sh World?Solution
Step 1: Understand script parameter usage
The script uses$1which is the first argument passed to the script. Here,Worldis passed as the first argument.Step 2: Predict output after substitution
The echo command prints "Hello, World!" by replacing$1with the argument.Final Answer:
Hello, World! -> Option BQuick Check:
Argument $1 replaced by 'World' = A [OK]
- Printing literal $1 instead of argument
- Missing argument causing empty output
- Assuming syntax error due to shebang
./myscript.sh but get Permission denied. What is the most likely fix?Solution
Step 1: Identify cause of 'Permission denied'
This error usually means the script file does not have execute permission.Step 2: Fix permission issue
Usechmod +x myscript.shto add execute permission, allowing the script to run.Final Answer:
Add execute permission withchmod +x myscript.sh. -> Option AQuick Check:
Permission denied fixed by chmod +x = D [OK]
- Trying to run script again without changing permissions
- Renaming script does not fix permissions
- Deleting script is unnecessary
deploy.sh located in /home/user/scripts from anywhere in the terminal without typing the full path. What is the best way to do this?Solution
Step 1: Understand PATH environment variable
PATH lists directories where the shell looks for commands. Adding a directory lets you run scripts inside it by name.Step 2: Add script directory to PATH
By adding/home/user/scriptsto PATH, you can rundeploy.shfrom anywhere simply by typingdeploy.sh.Final Answer:
Add/home/user/scriptsto the PATH environment variable. -> Option DQuick Check:
PATH addition enables running scripts anywhere = C [OK]
- Copying script to /tmp is temporary and not recommended
- Running with full path every time is inconvenient
- Renaming script does not affect running from anywhere
Ready to prove it?
Pass the test and this pattern moves unknown
