Skip to content

Instantly share code, notes, and snippets.

@yucelkilic
Forked from F1LT3R/progress-bar.sh
Last active February 3, 2017 23:07
Show Gist options
  • Select an option

  • Save yucelkilic/ef1c4f0ce5cebb225c9f0c885e837aeb to your computer and use it in GitHub Desktop.

Select an option

Save yucelkilic/ef1c4f0ce5cebb225c9f0c885e837aeb 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
@yucelkilic
Copy link
Author

If you get "invalid number" error from terminal, please take a look at this instructions.

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