Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Created April 14, 2017 08:16
Show Gist options
  • Save jdmichaud/3771dd677a695611258a64872165e22f to your computer and use it in GitHub Desktop.
Save jdmichaud/3771dd677a695611258a64872165e22f to your computer and use it in GitHub Desktop.
Progress bar in the shell
#!/bin/bash
function pb_init() {
# define the progress bar size (in characters)
PROGRESS_BAR_SIZE=20
# Hide the cursor
printf "\033[?25l"
# Show cursor on exit
trap 'printf "\033[?25h"' 0
}
function pb_progress() {
local progress
local remaining
local completed
local uncompleted
progress=$(echo "$PROGRESS_BAR_SIZE * $1 / $2" | bc)
remaining=$(echo "$PROGRESS_BAR_SIZE - $progress" | bc)
# Create string with ${progress} spaces in it
completed=$(printf "%${progress}s")
# Create string with ${remaining} spaces in it
uncompleted=$(printf "%${remaining}s")
# Substitute spaces with # or -
# See http://tldp.org/LDP/abs/html/string-manipulation.html
printf "[${completed// /#}${uncompleted// /-}]\r"
}
# This is user defined and is the expected number of iterations
end_value=153
pb_init
for current_value in $(seq 1 $end_value)
do
pb_progress $current_value $end_value
# Do something
sleep 0.1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment