How to Create a Progress Bar in Bash

Olorunfemi Akinlua Feb 02, 2024
  1. Use the pv Command to Create a Progress Bar in Bash
  2. Use the dialog Command to Create a Progress Bar in Bash
  3. Use the ncurses Library to Create a Progress Bar in Bash
How to Create a Progress Bar in Bash

A progress bar is a visual indicator showing the progress of a task, such as a long-running script or command. It can be useful for providing feedback to the user about the status of the task, and it can also help to estimate the time remaining until the task is complete.

This article will explore several approaches to adding a progress bar to a shell script in Bash, the default shell for Linux and macOS.

Use the pv Command to Create a Progress Bar in Bash

One of the simplest ways to add a progress bar to a shell script is to use the pv command, which stands for pipe viewer. This command allows monitoring of the progress of data passed through a pipe, which can be used to display a progress bar in the terminal.

For example, to add a progress bar to a script that copies a large file from one location to another, you can use the following command:

cat file.txt | pv -s $(stat -c%s file.txt) > /destination/file.txt

This command uses cat to read the contents of the file.txt file and pipes the output to the pv command. The pv command uses the -s option to specify the input data size in bytes and calculates the progress based on this value.

The output of the pv command is then redirected to the destination file.

When the script runs, the pv command will display a progress bar in the terminal, showing the percentage of the file copied and an estimated time remaining until the task is complete. The progress bar will update dynamically as the data is transferred and disappear when the task is finished.

Use the dialog Command to Create a Progress Bar in Bash

Another approach for adding a progress bar to a shell script is to use the dialog command, a utility for creating user-friendly interfaces in the terminal. The dialog command can display various widgets, including a progress bar, and it can be controlled through the script to show the current progress of the task.

To use the dialog command to add a progress bar to a shell script, you can use the --gauge option to create a new progress bar widget. The --gauge option takes several arguments, including the title of the widget, the height and width of the widget, and the initial value of the progress bar.

Here is an example of how to use the --gauge option to create a progress bar with a title of Copying file and an initial value of 0:

dialog --gauge "Copying file" 10 70 0

This command will create a progress bar widget with a height of 10 lines and a width of 70 characters, and it will display the title Copying file at the top of the widget. The progress bar will initially be empty, with a value of 0.

To update the progress of the task and the value of the progress bar, you can use the --title option to set the title of the widget and the --gauge-data option to set the progress bar’s current value. For example, to update the progress bar to show 50% complete, you can use the following command:

dialog --title "Copying file" --gauge-data 50

This command will update the widget’s title to Copying file and the value of the progress bar to 50. The progress bar will be redrawn with the new values, and the user will see the updated progress of the task in the terminal.

To use the dialog command in a shell script, you can use the --and-widget option to specify a list of widget commands that should be executed in sequence. This allows you to create and update the progress bar in the script, and it will ensure that the widgets are drawn and updated in the correct order.

Here is an example of a shell script that uses the dialog command to create and update a progress bar:

#!/bin/bash

# Create the progress bar widget
dialog --gauge "Copying file" 10 70 0

Use the ncurses Library to Create a Progress Bar in Bash

Another approach for adding a progress bar to a shell script is to use the ncurses library, a programming library for creating user-friendly interfaces in the terminal. The ncurses library provides many functions and widgets for creating interactive menus, forms, and other graphical user interfaces, and it can be used to create a custom progress bar in a shell script.

To use the ncurses library to add a progress bar to a shell script, you can use the initscr function to initialize the screen and the newwin function to create a new window for the progress bar. The newwin function takes several arguments, including the window’s height and width and the window’s initial position on the screen.

Here is an example of how to use the newwin function to create a progress bar window with a height of 10 lines and a width of 70 characters at the position (5,5) on the screen:

#!/bin/bash

# Initialize the screen
initscr

# Create the progress bar window
win=`newwin 10 70 5 5`

This code will initialize the screen and create a new window for the progress bar. The win variable will reference the window, which can be used to access and update the progress bar in the script.

To draw the initial state of the progress bar, you can use the box function to draw a box around the window and the mvwaddstr function to write the title of the progress bar and the initial value of 0% inside the box.

Here is an example of how to use these functions to draw the initial state of the progress bar:

# Draw the box around the window
box $win 0 0

# Write the title and initial value of the progress bar
mvwaddstr $win 1 1 "Copying file"
mvwaddstr $win 1 30 "0%"

To update the progress of the task and the value of the progress bar, you can use the mvwaddstr function again to write the new value of the progress bar inside the box. For example, to update the progress bar to show 50% complete, you can use the following code:

# Update the value of the progress bar
mvwaddstr $win 1 30 "50%"

This code will overwrite the existing value of the progress bar with the new value of 50%. The progress bar will be redrawn with the new value, and the user will see the updated progress of the task in the terminal.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn