Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Progress indicators in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a progress indicator in a bash script?
easy
A. To print all output to a file instead of the screen
B. To speed up the execution of the script
C. To stop the script if an error occurs
D. To show the user that a long-running task is still working

Solution

  1. Step 1: Understand what progress indicators do

    Progress indicators provide visual feedback during long tasks to reassure users the script is running.
  2. Step 2: Identify the correct purpose

    Among the options, only showing the user the task is still working matches the purpose of progress indicators.
  3. Final Answer:

    To show the user that a long-running task is still working -> Option D
  4. Quick Check:

    Progress indicator purpose = show task progress [OK]
Hint: Progress indicators show task status, not speed or errors [OK]
Common Mistakes:
  • Thinking progress indicators speed up scripts
  • Confusing progress indicators with error handling
  • Assuming progress indicators redirect output
2. Which of the following bash code snippets correctly updates the same line to show a spinner?
easy
A. echo -ne "|\r"; sleep 1; echo -ne "/\r"
B. echo "|"; sleep 1; echo "/"
C. echo -n "|"; sleep 1; echo -n "/"
D. echo -e "|\n"; sleep 1; echo -e "/\n"

Solution

  1. Step 1: Understand how to update the same line in bash

    Using echo -ne with \r returns the cursor to the start of the line to overwrite it.
  2. Step 2: Check each option for correct usage

    echo -ne "|\r"; sleep 1; echo -ne "/\r" uses echo -ne "|\r" and echo -ne "/\r", which updates the same line. Others print new lines or lack -e or -n.
  3. Final Answer:

    echo -ne "|\r"; sleep 1; echo -ne "/\r" -> Option A
  4. Quick Check:

    Use echo -ne with \r to update line [OK]
Hint: Use echo -ne and \r to overwrite the same line [OK]
Common Mistakes:
  • Using echo without -n or -e flags
  • Using \n instead of \r to update line
  • Printing spinner on new lines instead of same line
3. What will be the output of this bash snippet?
for i in {1..3}; do
  echo -ne "Processing $i...\r"
  sleep 1
done
echo "Done!"
medium
A. Processing 1... Processing 2... Processing 3... Done!
B. Processing 3... Done!
C. Processing 1...Processing 2...Processing 3...Done!
D. Done! Processing 3...

Solution

  1. 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.
  2. Step 2: Determine final visible output after loop

    Only the last 'Processing 3...' remains visible, then 'Done!' prints on a new line.
  3. Final Answer:

    Processing 3... Done! -> Option B
  4. Quick Check:

    \r overwrites line, last iteration visible [OK]
Hint: Remember \r returns cursor, overwriting previous line [OK]
Common Mistakes:
  • Thinking all lines print separately
  • Confusing \r with \n
  • Ignoring that echo -ne prevents newline
4. Identify the error in this bash spinner script snippet:
spinner="|/-\\"
while true; do
  for i in $(seq 0 3); do
    echo -ne "${spinner:$i:1}\r"
    sleep 0.2
done
done
medium
A. The script will cause an infinite loop without exit
B. The for loop uses incorrect sequence range
C. The spinner string is missing characters
D. The substring syntax ${spinner:$i:1} is invalid in bash

Solution

  1. Step 1: Analyze the loop structure

    The outer while true loop runs forever without any exit condition.
  2. Step 2: Check for exit or break statements

    No break or exit is present, so the spinner runs infinitely, which may be unintended.
  3. Final Answer:

    The script will cause an infinite loop without exit -> Option A
  4. Quick Check:

    while true without break causes infinite loop [OK]
Hint: Check loops for exit conditions to avoid infinite loops [OK]
Common Mistakes:
  • Assuming infinite loops are errors always
  • Confusing substring syntax with error
  • Ignoring that spinner string is correct
5. You want to create a progress bar in bash that fills from 0% to 100% in 10 steps, updating on the same line. Which approach correctly implements this?
hard
A. Use echo without -n or -e flags inside the loop to print progress
B. Print all '#' characters at once, then sleep 10 seconds
C. Use a loop from 1 to 10, print '#' repeated i times with echo -ne and \r, then sleep
D. Use multiple echo commands without \r to print progress on new lines

Solution

  1. Step 1: Understand progress bar update method

    To update on the same line, use echo -ne with \r to overwrite the line each iteration.
  2. Step 2: Implement loop with repeated '#' characters

    Loop from 1 to 10, print '#' repeated i times, then sleep to simulate progress.
  3. Final Answer:

    Use a loop from 1 to 10, print '#' repeated i times with echo -ne and \r, then sleep -> Option C
  4. Quick Check:

    Loop + echo -ne + \r = progress bar update [OK]
Hint: Use echo -ne with \r and repeated chars in loop [OK]
Common Mistakes:
  • Printing progress on new lines instead of same line
  • Not using echo -n or -e flags
  • Printing all progress at once without updates
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard