Issue
I want to show a progress bar during a bash operation in a specific format something similar to the following:
[###########](40%)
[###############](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.
Solution
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 %
Answered By - fedorqui 'SO stop harming' Answer Checked By - Cary Denson (WPSolving Admin)