Introduction
Progress indicators show users that a task is running and how much is done. They make waiting less boring and give feedback.
# Simple spinner example while :; do for c in '-' '\\' '|' '/'; do printf '\r%c' "$c" sleep 0.1 done done
# Spinner showing progress while :; do for c in '-' '\\' '|' '/'; do printf '\rProcessing... %c' "$c" sleep 0.2 done done
# Progress bar example for i in {1..20}; do sleep 0.1 printf '\r[' for ((j=0; j<i; j++)); do printf '='; done for ((j=i; j<20; j++)); do printf ' '; done printf '] %d%%' $((i * 5)) done printf '\n'
#!/bin/bash # Simple spinner while sleeping spin='-|/\\' printf 'Working... ' for i in {1..20}; do printf '\rWorking... %c' "${spin:i%4:1}" sleep 0.1 done printf '\nDone!\n'
echo -ne with \r returns the cursor to the start of the line to overwrite it.echo -ne "|\r" and echo -ne "/\r", which updates the same line. Others print new lines or lack -e or -n.for i in {1..3}; do
echo -ne "Processing $i...\r"
sleep 1
done
echo "Done!"spinner="|/-\\"
while true; do
for i in $(seq 0 3); do
echo -ne "${spinner:$i:1}\r"
sleep 0.2
done
donePass the test and this pattern moves unknown