Skip to content

Instantly share code, notes, and snippets.

@Templum
Created March 21, 2023 15:41
Show Gist options
  • Save Templum/604d0a7b684393ebeb1cc3b0592950f6 to your computer and use it in GitHub Desktop.
Save Templum/604d0a7b684393ebeb1cc3b0592950f6 to your computer and use it in GitHub Desktop.
Shell Progressbar Snippet
#!/bin/sh
bar_size=60
bar_char_done="#"
bar_char_todo="-"
bar_percentage_scale=2
function show_progress {
current="$1"
total="$2"
# calculate the progress in percentage
percent=$(bc <<< "scale=$bar_percentage_scale; 100 * $current / $total" )
# The number of done and todo characters
done=$(bc <<< "scale=0; $bar_size * $percent / 100" )
todo=$(bc <<< "scale=0; $bar_size - $done" )
# build the done and todo sub-bars
done_sub_bar=$(printf "%${done}s" | tr " " "${bar_char_done}")
todo_sub_bar=$(printf "%${todo}s" | tr " " "${bar_char_todo}")
# output the bar
echo "Progress : [${done_sub_bar}${todo_sub_bar}] ${percent}%\r\c"
if [ $total -eq $current ]; then
echo "\nDONE"
fi
}
# Call Example
idx=0
length=10
show_progress $idx $length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment