Skip to content

Instantly share code, notes, and snippets.

@stevenrombauts
Created February 26, 2014 14:37
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save stevenrombauts/9230513 to your computer and use it in GitHub Desktop.
Save stevenrombauts/9230513 to your computer and use it in GitHub Desktop.
Bash Progress Bar
#!/bin/bash
# Slick Progress Bar
# Created by: Ian Brown (ijbrown@hotmail.com)
# Please share with me your modifications
# Note: From http://stackoverflow.com/questions/11592583/bash-progress-bar
# Functions
PUT(){ echo -en "\033[${1};${2}H";}
DRAW(){ echo -en "\033%";echo -en "\033(0";}
WRITE(){ echo -en "\033(B";}
HIDECURSOR(){ echo -en "\033[?25l";}
NORM(){ echo -en "\033[?12l\033[?25h";}
function showBar {
percDone=$(echo 'scale=2;'$1/$2*100 | bc)
halfDone=$(echo $percDone/2 | bc) #I prefer a half sized bar graph
barLen=$(echo ${percDone%'.00'})
halfDone=`expr $halfDone + 6`
tput bold
PUT 7 28; printf "%4.4s " $barLen% #Print the percentage
PUT 5 $halfDone; echo -e "\033[7m \033[0m" #Draw the bar
tput sgr0
}
# Start Script
clear
HIDECURSOR
echo -e ""
echo -e ""
DRAW #magic starts here - must use caps in draw mode
echo -e " PLEASE WAIT WHILE SCRIPT IS IN PROGRESS"
echo -e " lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
echo -e " x x"
echo -e " mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
WRITE
#
# Insert your script here
for (( i=0; i<=50; i++ ))
do
showBar $i 50 #Call bar drawing function "showBar"
sleep .2
done
# End of your script
# Clean up at end of script
PUT 10 12
echo -e ""
NORM
@gohoyer
Copy link

gohoyer commented Dec 5, 2015

Thanks! This bar is very nice!

If you're interested, i've just made a little change to always print the whole bar and avoid skipping some parts of the bar if the progress is not regularly incremented.

https://gist.github.com/GustavoHoyer/6a361b732831ab0d7523/revisions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment