Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: Progress bar in bash

Tags:

linux

bash

I want to show a progress bar during a bash operation in a specific format something similar to the following:

[###########](40%)

after update it should become

[###############](50%)

and then similarly reach upto 100%

Is there any way to achieve this

I wrote the following bash program but i don't know how to show the percentage in this on same line:

#!/bin/bash
{
    echo -n "["
    for ((i = 0 ; i <= 100 ; i+=6)); do
        sleep 0.05
        echo -n "###"
    done
    echo -n "]"
    echo
}

Let's assume that a certain number of operations are being performed in a loop and after the completion of each operation, I want to report some progress. I don't want to install pv or any other utilities that does not come with default Ubuntu 12.04 installation.

like image 999
Ankit Kumar Avatar asked May 13 '14 11:05

Ankit Kumar


2 Answers

Let's use echo -n '...' $'\r' to print a carriage return:

for ((k = 0; k <= 10 ; k++))
do
    echo -n "[ "
    for ((i = 0 ; i <= k; i++)); do echo -n "###"; done
    for ((j = i ; j <= 10 ; j++)); do echo -n "   "; done
    v=$((k * 10))
    echo -n " ] "
    echo -n "$v %" $'\r'
    sleep 0.05
done
echo

It makes the cursor move to the beginning of the line to keep printing.

Output is as follows, always in the same line:

[ ##################                ] 50 % 

.../...

[ ################################# ] 100 % 
like image 187
fedorqui 'SO stop harming' Avatar answered Oct 03 '22 08:10

fedorqui 'SO stop harming'


Using printf:

for((i=0;i<=100;i+=6)); do
    printf "%-*s" $((i+1)) '[' | tr ' ' '#'
    printf "%*s%3d%%\r"  $((100-i))  "]" "$i"
    sleep 0.1
done; echo

Output: (in same line.. printed on different lines here for demo.)

[                                                                                                   ]  0%
[######                                                                                             ]  6%
[############                                                                                       ] 12%
[##################                                                                                 ] 18%
[########################                                                                           ] 24%
[##############################                                                                     ] 30%
[####################################                                                               ] 36%
[##########################################                                                         ] 42%
[################################################                                                   ] 48%
[######################################################                                             ] 54%
[############################################################                                       ] 60%
[##################################################################                                 ] 66%
[########################################################################                           ] 72%
[##############################################################################                     ] 78%
[####################################################################################               ] 84%
[##########################################################################################         ] 90%
[################################################################################################   ] 96%
like image 29
anishsane Avatar answered Oct 03 '22 06:10

anishsane