Progressbar in bash to visualize the time to wait
while true;do echo -n .;sleep 1;done &
sleep 10 # or do something else here
kill $!; trap 'kill $!' SIGTERM
echo done
this will start an infinite while loop that echos a spinner every second, executed in the background.
Instead of the sleep10
command run any command you want.
When that command finishes executing this will kill the last job running in the background (which is the infinite while loop)
source: https://stackoverflow.com/a/16348366/1069083
You can use various while loops instead, e.g. a spinner like this:
while :;do for s in / - \\ \|; do printf "\r$s";sleep 1;done;done
This should be enough to get you started:
#!/bin/bash
for i in {001..100}; do
sleep 1
printf "\r $i"
done
Using the \r
escape sequence returns the line to the start without a newline. This allows you to update the output without having hundreds of lines of output. By using this base, you could find a way to slowly print out an arrow such as =>25% ==>50% ===>75%
instead of simply printing a number out. You could do this in a very basic way by using if-then logic to print out a specific number of ='s depending on the size of the number.
Here's one using cursor movements that will rewrite the line in order to show a countdown:
c=5 # seconds to wait
REWRITE="\e[25D\e[1A\e[K"
echo "Starting..."
while [ $c -gt 0 ]; do
c=$((c-1))
sleep 1
echo -e "${REWRITE}$c"
done
echo -e "${REWRITE}Done."