JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Creating a Progress Bar in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

A progress bar is a visual indicator that shows the progress of a task, such as a long-running script or command. It can be used to provide feedback to the user about the status of a task and can also help estimate the time remaining before the task is completed.

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


Creating a Progress Bar in Bash Using the pv Command

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 you to monitor the progress of data passed through a pipe and can be used to display a progress bar in your terminal.

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

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

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

pvThe output of the command is then redirected to the target file.

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


Creating a progress bar in Bash using the dialog command

Another way to add 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 a variety of widgets, including a progress bar, and can be controlled from a script to show the current progress of a task.

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

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

dialog --gauge "Copying file" 10 70 0

This command will create a progress bar widget that is 10 lines high and 70 characters wide, and will display the title Copying Files at the top of the widget. The progress bar is initially empty and has 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 current value of the progress bar. For example, to update the progress bar to show 50% complete, you would use the following command:

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

This command will update the title of the widget to Copying Files and update the value of the progress bar to 50. The progress bar will redraw with the new value and the user will see the updated task progress 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 a script and will ensure that the widgets are drawn and updated in the correct order.

The following 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

Creating a progress bar in Bash using the ncurses library

Another way to add 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 custom progress bars in shell scripts.

To add a progress bar in a shell script using the ncurses library, 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 parameters, including the height and width of the window and the initial position of the window on the screen.

The following example illustrates 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 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 your 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% in 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 into the box. For example, to update the progress bar to show that it is 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 task progress in the terminal.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial