Skip to content

Progress Bar

In Shell

#!/bin/bash
TOTAL_COUNT=100
BAR_LENGTH=50

i=0
str="#"
ch=('|' '\' '-' '/')
index=0
while [  $i -le ${TOTAL_COUNT} ]
do
    printf "[%-${BAR_LENGTH}s][%d%%][%c]\r" $str $(($i*100/${TOTAL_COUNT})) ${ch[$index]}
    #Not good if $TOTAL_COUNT/$BAR_LENGTH != int
    if [ $(($i%($TOTAL_COUNT/$BAR_LENGTH))) -eq 0 ]; then
        str+='#'
    fi
    i=$(($i+1))
    index=$(($i%4))
    sleep 0.1
done
printf "\n"