0
0
Bash Scriptingscripting~10 mins

Progress indicators in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Progress indicators
Start Task
Initialize Progress
Loop: Update Progress
Display Progress Bar
Check Completion
v +----> Back to Loop
End Task
v +----> Back to Loop
The script starts a task, initializes progress, then loops updating and displaying the progress bar until the task completes.
Execution Sample
Bash Scripting
total=10
for i in $(seq 1 $total); do
  sleep 0.2
  percent=$((i * 100 / total))
  echo -ne "Progress: $percent%\r"
done
echo
This script shows a simple progress percentage updating in place as a loop runs.
Execution Table
IterationipercentActionOutput
1110Calculate percent and displayProgress: 10%
2220Calculate percent and displayProgress: 20%
3330Calculate percent and displayProgress: 30%
4440Calculate percent and displayProgress: 40%
5550Calculate percent and displayProgress: 50%
6660Calculate percent and displayProgress: 60%
7770Calculate percent and displayProgress: 70%
8880Calculate percent and displayProgress: 80%
9990Calculate percent and displayProgress: 90%
1010100Calculate percent and displayProgress: 100%
Exit--Loop ends after i=10Progress complete
💡 i reaches 10, loop ends, progress reaches 100%
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
i-1234567891010
percent-102030405060708090100100
Key Moments - 3 Insights
Why does the progress percentage overwrite the previous output instead of printing new lines?
Because the script uses 'echo -ne' with '\r' (carriage return) to return the cursor to the line start, so the new progress overwrites the old one (see execution_table rows 1-10).
What happens if the sleep command is removed?
The progress updates too fast to see, making the progress bar appear instantly complete (refer to execution_table timing implied by 'sleep 0.2').
Why is there an extra 'echo' after the loop?
To move the cursor to the next line after the progress finishes, so the prompt or next output doesn't overwrite the progress line (after exit row in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'percent' at iteration 5?
A40
B60
C50
D30
💡 Hint
Check the 'percent' column in the execution_table row for iteration 5.
At which iteration does the loop end according to the execution table?
A10
B9
C11
D5
💡 Hint
Look for the 'Exit' row and the last iteration number in the execution_table.
If the total variable was changed to 20, how would the 'percent' value at iteration 10 change?
A20
B50
C100
D10
💡 Hint
Percent is calculated as (i * 100 / total), so at i=10 and total=20, percent = 50.
Concept Snapshot
Progress indicators in bash:
- Use a loop to track progress steps
- Calculate percent: (current_step * 100 / total_steps)
- Use 'echo -ne' with '\r' to overwrite line
- Add 'sleep' to slow updates for visibility
- End with 'echo' to move cursor down
Full Transcript
This visual execution shows how a bash script can display a progress indicator by looping from 1 to total steps. Each iteration calculates the percent complete and prints it on the same line using carriage return '\r'. The sleep command slows the loop so the progress is visible. The loop ends when the counter reaches the total, and a final echo moves the cursor to the next line. Variables 'i' and 'percent' update each iteration, showing progress from 10% to 100%. This method helps users see task progress in the terminal without cluttering output.