Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Bash Tips & Tricks»Creating Progress Bar in Bash

    Creating Progress Bar in Bash

    By RahulJune 25, 20233 Mins Read

    As a system administrator, developer, or tech enthusiast, you will often find yourself interacting with Bash or another Unix shell. For tasks that may take a long time to execute, it’s helpful to provide a progress bar or some kind of indication to let you know that the script is still running and how far it has gotten. This article offers a hands-on approach to creating a progress bar using Bash scripting.

    What is a Progress Bar?

    A progress bar is a visual indicator used in the computing environment to show the progress of an operation. It’s a user-friendly way to represent the completion status of a process or task that might otherwise seem ambiguous or lengthy.

    Why Use a Bash Progress Bar?

    Implementing a progress bar in your Bash script offers multiple benefits:

    • Feedback: It provides feedback that a process is still running and has not been halted or stuck.
    • Estimation: It provides an estimated completion time, helping to manage expectations for how long a task will take.
    • Usability: It improves the overall user experience by making long-running scripts more interactive and less monotonous.

    Creating a Basic Progress Bar in Bash

    Let’s start with a simple approach. Here’s a basic script that displays a progress bar:

    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash
     
    echo -ne '#####                     (33%)\r'
    sleep 1
    echo -ne '#############             (66%)\r'
    sleep 1
    echo -ne '#######################   (100%)\r'
    echo -ne '\n'

    This script uses the echo command to output the progress bar and the percentage of progress. The `-ne` options tell echo to not output a trailing newline and to interpret escape sequences.

    The `\r` escape sequence returns the cursor to the beginning of the line, effectively allowing us to overwrite the line each time `echo` is called.

    Advanced Bash Progress Bar

    The previous example is quite basic and doesn’t dynamically update based on a process’s actual progress. Let’s create a more advanced progress bar that fills up over a specified duration.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #!/bin/bash
     
    # Function to display the progress bar
    progress_bar() {
      local duration=${1}
        
      already_done() { for ((done=0; done<$1; done++)); do printf "#"; done }
      remaining() { for ((remain=$1; remain<$duration; remain++)); do printf " "; done }
      percentage() { printf "| %s%%" $(( (($1)*100)/($duration)*100/100 )); }
      clean_line() { printf "\r"; }
     
      for (( current_duration=1; current_duration<=$duration; current_duration++ )); do
        already_done $current_duration
        remaining $current_duration
        percentage $current_duration
        clean_line
        sleep 1
      done
     
      clean_line
    }
     
    # Call the function with the specific duration
    progress_bar 10

    This script defines a function `progress_bar` that takes a single argument: the total duration of the operation. It uses several helper functions to draw the progress bar, calculate the remaining time, and display the percentage of completion.

    Conclusion

    A progress bar is a useful tool for improving the usability of long-running Bash scripts. This article showed you how to create a simple progress bar and then build a more advanced, dynamic one.

    Remember that these are just examples, and you can modify them according to your requirements. Adding a progress bar to your Bash scripts helps provide clear, visual feedback for your users and can enhance the overall user experience.

    As you continue scripting in Bash, you’ll discover many other ways to provide user-friendly feedback during script execution. The progress bar is just one of the many powerful tools you have at your disposal when it comes to interactive Bash scripting. Happy scripting!

    bash Progress Bar
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Assignment Operators in Bash

    An Introduction to Bash Variables

    An Introduction to Bash Variables

    Bash LOCAL and GLOBAL Variables

    Bash LOCAL and GLOBAL Variables

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Ubuntu 24.04 LTS: The Future of Open-Source Excellence
    • How to Execute Linux Commands in Python
    • Creating MySQL User with GRANT OPTION
    • Where to find crontab (cron) logs in Ubuntu & Debian
    • Backing Up Docker Volumes and Upload to S3
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.