Skip to content

Instantly share code, notes, and snippets.

@F1LT3R
Created January 22, 2016 14:20
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save F1LT3R/fa7f102b08a514f2c535 to your computer and use it in GitHub Desktop.
Save F1LT3R/fa7f102b08a514f2c535 to your computer and use it in GitHub Desktop.
Bash Progress Bar
#!/bin/bash
# Bash Progress Bar: https://gist.github.com/F1LT3R/fa7f102b08a514f2c535
progressBarWidth=20
# Function to draw progress bar
progressBar () {
# Calculate number of fill/empty slots in the bar
progress=$(echo "$progressBarWidth/$taskCount*$tasksDone" | bc -l)
fill=$(printf "%.0f\n" $progress)
if [ $fill -gt $progressBarWidth ]; then
fill=$progressBarWidth
fi
empty=$(($fill-$progressBarWidth))
# Percentage Calculation
percent=$(echo "100/$taskCount*$tasksDone" | bc -l)
percent=$(printf "%0.2f\n" $percent)
if [ $(echo "$percent>100" | bc) -gt 0 ]; then
percent="100.00"
fi
# Output to screen
printf "\r["
printf "%${fill}s" '' | tr ' ' ▉
printf "%${empty}s" '' | tr ' ' ░
printf "] $percent%% - $text "
}
## Collect task count
taskCount=33
tasksDone=0
while [ $tasksDone -le $taskCount ]; do
# Do your task
(( tasksDone += 1 ))
# Add some friendly output
text=$(echo "somefile-$tasksDone.dat")
# Draw the progress bar
progressBar $taskCount $taskDone $text
sleep 0.01
done
echo
@nlpthk
Copy link

nlpthk commented Jan 11, 2017

Hey F1LT3R!

How I could use your progress bar to represent a running bash file? Also, any idea how to add time remaining?
Thanks so much for your help!

@oofnikj
Copy link

oofnikj commented May 22, 2017

Hi,
I was having a problem with the Unicode characters not displaying properly (Ubuntu 16.04). I found out that it's a known bug with GNU tr that it doesn't support Unicode (source), so I modified it to work with sed instead:
27 printf "%${fill}s" '' | sed 's/ /\o342\o226\o210/g'
28 printf "%${empty}s" '' | sed 's/ /\o342\o226\o221/g'

Hope this helps someone.

@F1LT3R
Copy link
Author

F1LT3R commented Jul 14, 2017

@nlpthk, not sure what you are asking exactly

@glaiel
Copy link

glaiel commented May 13, 2020

@oofnikj Thanks, it worked perfectly for Ubuntu 18.04.

@MaKaNu
Copy link

MaKaNu commented May 26, 2023

Hi,

in line 7 and 16 the printf should be extended to support systems with different number encoding:

-7 fill=$(printf "%.0f\n" $progress)
+7 fill=$(LC_NUMERIC="en_US.UTF-8"  printf "%.0f\n" $progress)

-16 percent=$(printf "%0.2f\n" $percent)
+16 percent=$(LC_NUMERIC="en_US.UTF-8" printf "%0.2f\n" $percent)

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