Created
January 27, 2026 14:58
-
-
Save ConnerWill/94a657cea811ea1287aa9e032a8aa143 to your computer and use it in GitHub Desktop.
Bash progress bar script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env bash | |
| ### FUNCTION ###{{{ | |
| function bash_progress_bar(){ | |
| ## Localize Variables ##{{{ | |
| local \ | |
| message \ | |
| bar \ | |
| color \ | |
| bar_color_fg_setup \ | |
| bar_color_bg_setup \ | |
| bar_color \ | |
| bar_color_reset \ | |
| sleep_time \ | |
| left | |
| ##}}} | |
| ## Define Variables ##{{{ | |
| ## Define Config Variables ##{{{ | |
| sleep_time=0.02 | |
| color="93m" #White:"255m" #Red:"196m" #Blue:"21m" #Yellow:"190m" #Lime:"46m" #Magenta:"201m" | |
| message_color='\e[0;1;38;5;255m' #Magenta Background Bold White Foreground \e[0;1;38;5;255;48;5;201m | |
| progress_color='\e[0;38;5;7m' #White Background Bold Magenta Foreground \e[0;1;38;5;201;48;5;255m | |
| ##}}} | |
| ## Define Static Variables ##{{{ | |
| [[ -z "${message}" ]] && message="${*}" | |
| bar='' | |
| bar_color_fg_setup="\e[0;38;5;$color" | |
| bar_color_bg_setup="\e[48;5;$color" | |
| bar_color="${bar_color_fg_setup}${bar_color_bg_setup}" | |
| bar_color_reset='\e[0m' | |
| color_reset='\e[0m' | |
| ##}}} | |
| ##}}} | |
| ## Help Menu ##{{{ | |
| ## Define Help Menu Variables ##{{{ | |
| help_color_title='\e[0;1;4;38;5;4m' | |
| help_color_command='\e[0;38;5;10m' | |
| help_color_option='\e[0;3;38;5;11m' | |
| help_color_option_sep='\e[0;38;5;7m' | |
| help_color_description='\e[0;3;38;5;7m' | |
| help_color_dollar='\e[0;38;5;8m' | |
| ##}}} | |
| ## Display Help Menu ##{{{ | |
| if [[ "${message}" == "-h" ]] || [[ "${message}" == "--help" ]]; then | |
| printf " | |
| ${help_color_title}NAME${color_reset} | |
| ${help_color_command}%s${color_reset} | |
| ${help_color_title}USAGE${color_reset} | |
| ${help_color_command}%s${color_reset} ${help_color_option_sep}[${help_color_option}-h${help_color_option_sep}|${help_color_option}--help${help_color_option_sep}]${color_reset} ${help_color_option_sep}[${help_color_option}message${help_color_option_sep}]${color_reset} | |
| ${help_color_title}EXAMPLE${color_reset} | |
| ${help_color_description}Show progress bar with no message | |
| ${help_color_dollar}\$${color_reset} ${help_color_command}%s${color_reset} | |
| ${bar_color}█████████████${bar_color_reset} ${progress_color}19%%${bar_color_reset} | |
| ${help_color_title}EXAMPLE${color_reset} | |
| ${help_color_description}Show progress bar with the message: 'Loading' | |
| ${help_color_dollar}\$${color_reset} ${help_color_command}%s${color_reset} ${help_color_option}\"Loading\"${color_reset} | |
| ${message_color}Loading:${bar_color_reset}${bar_color}█████████████████████████${bar_color_reset} ${progress_color}46%%${bar_color_reset} | |
| ${help_color_title}EXAMPLE${color_reset} | |
| ${help_color_description}Show progress bar with the message: 'Current Progress' using no quotes | |
| ${help_color_dollar}\$${color_reset} ${help_color_command}%s${color_reset} ${help_color_option}Current Progress${color_reset} | |
| ${message_color}Current Progress:${bar_color_reset}${bar_color}█████████████████████████████████████████████████${bar_color_reset} ${progress_color}91%%${bar_color_reset} | |
| \n" "${0}" "${0}" "${0}" "${0}" "${0}" | |
| return | |
| fi | |
| ##}}} | |
| ##}}} | |
| ## Define message Variables ##{{{ | |
| [[ -n "${message}" ]] && message="${*}:" | |
| ##}}} | |
| ## Set Traps ##{{{ | |
| trap 'printf "\e[?25h\e[2K\r"; return' SIGHUP SIGINT SIGQUIT SIGABRT | |
| ##}}} | |
| ## Start Line/Cursor Effects ##{{{ | |
| printf "\e[?25l" ## Hide Cursor | |
| ##}}} | |
| ## Progress Bar Loop ##{{{ | |
| for (( x=0; x <= 100; x++ )); do | |
| sleep $sleep_time ## Sleep | |
| bar="${bar} " ## Redefine Bar | |
| left="$(( 100 - x ))" ## Calculate/Define Left | |
| printf "\r${message_color}%s${bar_color}%s${bar_color_reset}" "${message}" "${bar}" ## Print Bar | |
| printf " %${left}s" ## Print Left | |
| printf "${progress_color}%s%%${bar_color_reset}" "${x}" ## Print Left | |
| done | |
| ##}}} | |
| ## End Line/Cursor Effects ##{{{ | |
| printf "\e[?25h" ## Restore Cursor | |
| printf "\n" ## New Line | |
| ##}}} | |
| } | |
| ###}}} | |
| ### CALL FUNCTION ###{{{ | |
| ## Call 'progress_bar' Function ##{{{ | |
| bash_progress_bar "${@}" | |
| ##}}} | |
| ###}}} | |
| ### HELP ###{{{ | |
| # | |
| # NAME | |
| # | |
| # ./bash_progress_bar.sh | |
| # | |
| # | |
| # USAGE | |
| # | |
| # ./bash_progress_bar.sh [-h|--help] [message] | |
| # | |
| # | |
| # EXAMPLE | |
| # | |
| # Show progress bar with no message | |
| # | |
| # $ ./bash_progress_bar.sh | |
| # | |
| # █████████████ 19% | |
| # | |
| # | |
| # EXAMPLE | |
| # | |
| # Show progress bar with the message: 'Loading' | |
| # | |
| # $ ./bash_progress_bar.sh "Loading" | |
| # | |
| # Loading:█████████████████████████ 46% | |
| # | |
| # | |
| # EXAMPLE | |
| # | |
| # Show progress bar with the message: 'Current Progress' using no quotes | |
| # | |
| # $ ./bash_progress_bar.sh Current Progress | |
| # | |
| # Current Progress:█████████████████████████████████████████████████ 91% | |
| # | |
| ###}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment