How to Add a Progress Bar in Shell Script

Introduction:

When it is required to wait for a fixed amount of time during the execution of a script, it is better to Add a Progress Bar in Shell Script to inform the user to wait for some time. The progress bar can be created using a simple Bash script or using some built-in Linux cohttps://linuxhints.info/mmands such as “cv”, “dialog”, etc. The methods of creating a progress bar using a Bash script are shown in this tutorial.

The Requirements:

A typical command line progress bar may look like this:

Progress : [#################--------------------------------] 35.25%

It is composed of two components: a percentage figure and a textual progress bar. The progress meter should also automatically reload. Stated differently, the same output line should always be overwritten by the output.

Allowing the user to change the progress bar’s size, the percentage value’s scale, and the characters that represent done and todo—# and – in the example above—will make it easier for users to modify the bar.

 

Other than that, reusability would be a crucial need. Our progress indicator should ideally be a function. Whatever type of activity they are working on, users can call it to display progress information without making any changes.

Finally, let’s summarize the requirements we need to achieve:

  • refreshing the same line
  • customizable – bar size, “done” and “todo” characters on the bar, and the percentage scale
  • reusable – easy to be called by various tasks without modifying the code

What is a Progress Bar?

A Progress Bar in Shell Script is a visual indicator used in computing systems to show the current state of an operation. It’s a straightforward method of displaying the current status of an activity or process, which can otherwise be difficult to understand or time-consuming.

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:

The progress meter and the progress % are output by this script using the echo command. The {-ne} options instruct echo to parse escape sequences and not produce a trailing newline.

The escape sequence {\r} allows us to essentially overwrite the line every time {echo} is called by bringing the cursor back to the beginning of the line.

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.

 

The function {progress_bar} in this script has a single argument, which is the whole operation duration. To draw the progress bar, figure out how much time is left, and show the completion percentage, it makes use of a number of utility methods.

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.

 

Get more information about
Parallel Jobs in Bash Using the For Loop

Leave a Reply

Your email address will not be published. Required fields are marked *