Bash Programming – How to Create a Progress Bar to Visualize Wait Time

bashprogramming

In a bash script sometimes you need the user to wait some seconds for a background process to finish.

I usually use for example:

sleep 10

How can I add a kind of progressbar to the script, so the user knows how long to wait?

I installed the command bar but I don't understand the manual.

Best Answer

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
Related Question