Skip to content

Instantly share code, notes, and snippets.

@LiamFry
Created August 27, 2025 06:47
Show Gist options
  • Select an option

  • Save LiamFry/dd4f6186b883e5bfb8a7d4ed954f7bb1 to your computer and use it in GitHub Desktop.

Select an option

Save LiamFry/dd4f6186b883e5bfb8a7d4ed954f7bb1 to your computer and use it in GitHub Desktop.
Possible speedup for "animated progress bar in bash"
#!/usr/bin/env bash
BATCHSIZE=1
BAR_CHAR='|'
PAD_CHAR=' '
LENGTH=50
BAR_POOL=$(eval "printf \"${BAR_CHAR}%.0s\" {1..$LENGTH}")
PAD_POOL=$(eval "printf \"${PAD_CHAR}%.0s\" {1..$LENGTH}")
# ##############################################################################
progress-bar() {
local current=$1
local len=$2
local perc_done=$((current * 100 / len))
local num_bars=$((perc_done * LENGTH / 100))
echo -ne "[${BAR_POOL:0:num_bars}${PAD_POOL:0:LENGTH-num_bars}] $current/$len ($perc_done%)\r"
}
process-files() {
local files=("$@")
sleep .01
}
# ##############################################################################
shopt -s globstar nullglob
echo 'finding files'
files=(./**/*cache)
len=${#files[@]}
echo "found $len files"
for ((i = 0; i < len; i += BATCHSIZE)); do
progress-bar "$((i+1))" "$len"
process-files "${files[@]:i:BATCHSIZE}"
done
progress-bar "$len" "$len"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment