In this bash script code article you will see how to create a progress bar. This is a fine example to show download progress, loaders, waiting etc. This function accepts the current elapsed percentage and total length of the progress bar in characters.
In this example, we are running each iteration at a delay of 0.1 seconds. So, in 10 sec our progress bar will reach 100%.
Code Example –
#!/bin/bash bar() { ((elapsed=$1*$2/100)) # Create the bar with spaces. printf -v prog "%${elapsed}s" printf -v total "%$(($2-elapsed))s" printf '%s\r' "[${prog// /-}${total}]" } sleep() { read -rt "$1" <> <(:) || : } for ((i=0;i<=100;i++)); do sleep 0.1 # Print the bar. bar "$i" "10" done printf '\n' # Output: # [---- ]
Inspired from Dylan Araps