Creating a Progress Bar in Bash
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 cat
read the contents of the file.txt file and pipes the output to the pv command. pv
The command uses the -s option to specify the input data size in bytes and calculates the progress based on this value.
pv
The 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.
Related Articles
String comparison in batch files
Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM
-
A string is an ordered collection of characters. You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batc
Remove double quotes from variable in batch file
Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM
-
In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa
Reading file into variable in batch script
Publish Date:2025/03/22 Views:121 Category:OPERATING SYSTEM
-
Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in
Print a file after skipping first X lines in Bash
Publish Date:2025/03/22 Views:187 Category:OPERATING SYSTEM
-
Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li
Get the Primary IP Address in Linux
Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM
-
There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash
Bash History Size
Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM
-
In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve
Find Current Folder Name in Bash
Publish Date:2025/03/22 Views:107 Category:OPERATING SYSTEM
-
Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I
Changing Directories in Bash
Publish Date:2025/03/22 Views:76 Category:OPERATING SYSTEM
-
In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to
Running Regular Expressions in Case Statements in Bash
Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM
-
This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters