Progress indicators in Bash Scripting - Time & Space Complexity
When we add progress indicators in bash scripts, we want to know how much extra work they add as the task grows.
We ask: How does showing progress affect the script's running time as input size increases?
Analyze the time complexity of the following code snippet.
#!/bin/bash
count=0
for file in /path/to/files/*; do
# Process each file
echo "Processing $file"
((count++))
# Show progress every 10 files
if (( count % 10 == 0 )); then
echo "Processed $count files so far..."
fi
done
This script processes files one by one and prints a progress message every 10 files.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each file in the directory.
- How many times: Once per file, so as many times as there are files (n).
- Progress check: Every 10 files, a message is printed (about n/10 times).
As the number of files grows, the script processes each file once and prints progress messages occasionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file processes + 1 progress message |
| 100 | 100 file processes + 10 progress messages |
| 1000 | 1000 file processes + 100 progress messages |
Pattern observation: The main work grows linearly with n, and progress messages grow much slower, about n divided by 10.
Time Complexity: O(n)
This means the script's running time grows directly in proportion to the number of files processed.
[X] Wrong: "Printing progress every file makes the script much slower, changing the time complexity."
[OK] Correct: Printing progress messages adds some work, but it happens at fixed intervals, so it doesn't change the overall linear growth with input size.
Understanding how progress indicators affect script speed shows you can balance user feedback with efficiency, a useful skill in real scripting tasks.
"What if we printed a progress message after processing every single file instead of every 10 files? How would the time complexity change?"
Practice
Solution
Step 1: Understand what progress indicators do
Progress indicators provide visual feedback during long tasks to reassure users the script is running.Step 2: Identify the correct purpose
Among the options, only showing the user the task is still working matches the purpose of progress indicators.Final Answer:
To show the user that a long-running task is still working -> Option DQuick Check:
Progress indicator purpose = show task progress [OK]
- Thinking progress indicators speed up scripts
- Confusing progress indicators with error handling
- Assuming progress indicators redirect output
Solution
Step 1: Understand how to update the same line in bash
Usingecho -newith\rreturns the cursor to the start of the line to overwrite it.Step 2: Check each option for correct usage
echo -ne "|\r"; sleep 1; echo -ne "/\r" usesecho -ne "|\r"andecho -ne "/\r", which updates the same line. Others print new lines or lack-eor-n.Final Answer:
echo -ne "|\r"; sleep 1; echo -ne "/\r" -> Option AQuick Check:
Use echo -ne with \r to update line [OK]
- Using echo without -n or -e flags
- Using \n instead of \r to update line
- Printing spinner on new lines instead of same line
for i in {1..3}; do
echo -ne "Processing $i...\r"
sleep 1
done
echo "Done!"Solution
Step 1: Understand echo -ne with \r inside the loop
Each loop iteration prints 'Processing i...' and returns cursor to line start, overwriting previous output.Step 2: Determine final visible output after loop
Only the last 'Processing 3...' remains visible, then 'Done!' prints on a new line.Final Answer:
Processing 3... Done! -> Option BQuick Check:
\r overwrites line, last iteration visible [OK]
- Thinking all lines print separately
- Confusing \r with \n
- Ignoring that echo -ne prevents newline
spinner="|/-\\"
while true; do
for i in $(seq 0 3); do
echo -ne "${spinner:$i:1}\r"
sleep 0.2
done
doneSolution
Step 1: Analyze the loop structure
The outer while true loop runs forever without any exit condition.Step 2: Check for exit or break statements
No break or exit is present, so the spinner runs infinitely, which may be unintended.Final Answer:
The script will cause an infinite loop without exit -> Option AQuick Check:
while true without break causes infinite loop [OK]
- Assuming infinite loops are errors always
- Confusing substring syntax with error
- Ignoring that spinner string is correct
Solution
Step 1: Understand progress bar update method
To update on the same line, use echo -ne with \r to overwrite the line each iteration.Step 2: Implement loop with repeated '#' characters
Loop from 1 to 10, print '#' repeated i times, then sleep to simulate progress.Final Answer:
Use a loop from 1 to 10, print '#' repeated i times with echo -ne and \r, then sleep -> Option CQuick Check:
Loop + echo -ne + \r = progress bar update [OK]
- Printing progress on new lines instead of same line
- Not using echo -n or -e flags
- Printing all progress at once without updates
Ready to prove it?
Pass the test and this pattern moves unknown
