Skip to content

Instantly share code, notes, and snippets.

@issadarkthing
Created April 22, 2020 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save issadarkthing/f46108ff5206651e9dd96e6cf6b9300b to your computer and use it in GitHub Desktop.
Save issadarkthing/f46108ff5206651e9dd96e6cf6b9300b to your computer and use it in GitHub Desktop.
progress bar in bash
#!/bin/bash
# getProgressString <TOTAL ITEMS> <FILLED LOOK> <NOT FILLED LOOK> <STATUS> <DELIMETER>
# For instance:
# $ getProgressString 10 "#" "-" 50 "|"
# #####|-----
ITEMS="$1" # The total number of items(the width of the bar)
FILLED_ITEM="$2" # The look of a filled item
NOT_FILLED_ITEM="$3" # The look of a not filled item
STATUS="$4" # The current progress status in percent
DELIMETER="$5"
# calculate how many items need to be filled and not filled
FILLED_ITEMS=$(echo "((${ITEMS} * ${STATUS})/100 + 0.5) / 1" | bc)
NOT_FILLED_ITEMS=$(echo "$ITEMS - $FILLED_ITEMS" | bc)
# Assemble the bar string
msg=$(printf "%${FILLED_ITEMS}s$DELIMETER" | sed "s| |${FILLED_ITEM}|g")
msg=${msg}$(printf "%${NOT_FILLED_ITEMS}s" | sed "s| |${NOT_FILLED_ITEM}|g")
echo "$msg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment