0
0
Bash Scriptingscripting~20 mins

Progress indicators in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Progress Indicator Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this progress indicator script?
Consider this bash script snippet that simulates a progress bar:
for i in {1..5}; do
  echo -n "."
  sleep 1
done
echo " Done!"
What will be the output after running this script?
Bash Scripting
for i in {1..5}; do
  echo -n "."
  sleep 1
done
echo " Done!"
A..... Done!
B. . . . . Done!
CError: sleep command not found
D.....\nDone!
Attempts:
2 left
💡 Hint
Look at how echo -n works and how the loop prints dots.
📝 Syntax
intermediate
2:00remaining
Which option correctly implements a spinner progress indicator in bash?
You want to create a spinner that cycles through | / - \ characters in bash. Which of these code snippets correctly implements this spinner inside a loop?
Bash Scripting
spinner='|/-\\'
for i in {1..4}; do
  # print spinner character
  # wait 0.2 seconds
  # erase spinner character
  # move to next character
  sleep 0.2
done
Afor i in {0..3}; do echo -ne "${spinner:i:1}"; sleep 0.2; echo -ne "\b"; done
Bfor i in {0..3}; do echo -n "${spinner:i:1}"; sleep 0.2; echo -n "\b"; done
Cfor i in {0..3}; do echo -n "${spinner:i:1}"; sleep 0.2; echo -ne "\r"; done
Dfor i in {0..3}; do echo -ne "${spinner:i:1}"; sleep 0.2; echo -ne "\r"; done
Attempts:
2 left
💡 Hint
Use echo -ne to print without newline and interpret backslash sequences.
🔧 Debug
advanced
2:00remaining
Why does this progress bar script not update correctly?
This script is supposed to show a progress bar updating from 0% to 100%:
for i in $(seq 0 20 100); do
  echo -n "[${i}%]"
  sleep 1
done
echo
But it prints all progress states on separate lines instead of updating the same line. What is the main reason?
Aseq command outputs numbers with newlines causing echo to fail
Becho -n does not prevent newline, so each print goes to new line
Csleep command causes line breaks after each print
DMissing carriage return \r to return cursor to line start before printing
Attempts:
2 left
💡 Hint
Think about how to overwrite the same line in terminal output.
🚀 Application
advanced
3:00remaining
Which script shows a progress bar with dynamic width based on terminal size?
You want a bash script that shows a progress bar filling from 0% to 100%, adjusting bar width to terminal width. Which script correctly calculates terminal width and updates the bar accordingly?
A
width=$(tput cols)
for i in $(seq 0 10 100); do
  filled=$(( i / width * 100 ))
  empty=$(( width - filled ))
  printf "[%0.s#" $(seq 1 $filled)
  printf "%0.s-" $(seq 1 $empty)
  printf "] %d%%\r" $i
  sleep 0.5
done
echo
B
width=$(tput cols)
for i in $(seq 0 10 100); do
  filled=$(( width * 100 / i ))
  empty=$(( width - filled ))
  printf "[%0.s#" $(seq 1 $filled)
  printf "%0.s-" $(seq 1 $empty)
  printf "] %d%%\r" $i
  sleep 0.5
done
echo
C
width=$(tput cols)
for i in $(seq 0 10 100); do
  filled=$(( i * width / 100 ))
  empty=$(( width - filled ))
  printf "[%0.s#" $(seq 1 $filled)
  printf "%0.s-" $(seq 1 $empty)
  printf "] %d%%\r" $i
  sleep 0.5
done
echo
D
width=$(tput cols)
for i in $(seq 0 10 100); do
  filled=$(( width - i ))
  empty=$(( i ))
  printf "[%0.s#" $(seq 1 $filled)
  printf "%0.s-" $(seq 1 $empty)
  printf "] %d%%\r" $i
  sleep 0.5
done
echo
Attempts:
2 left
💡 Hint
Calculate filled length as percentage of terminal width.
🧠 Conceptual
expert
1:30remaining
What is the main benefit of using \r (carriage return) in progress indicators?
In bash scripting, why is the carriage return character \r important when creating progress indicators?
AIt clears the entire terminal screen before printing new output
BIt moves the cursor to the beginning of the current line allowing overwriting previous output
CIt moves the cursor to the next line to print progress on a new line
DIt pauses the script execution until user input is received
Attempts:
2 left
💡 Hint
Think about how to update progress on the same line without clutter.