Different Examples of Creating a Progress Bar in Bash
The different ways of implementing a progress bar in Bash are shown in this part of the tutorial.
Example 1: Implement a Simple Progress Bar without Any Command
Create a Bash file with the following script that displays a progress bar using the “#” character and the “sleep” command. The “printf” command is used here to display the progress bar. The progress bar is divided into four parts. The 25% is displayed after 1 second. The 50% is displayed after 3 seconds. The 75% is displayed after 2 seconds. The 100% is displayed after 1 second.
printf "\nWait to complete the task...\n\n"
#Wait for 1 second
sleep 1
#Print the first part of the progress bar
printf "[##### ] 25%% completed.\r"
#Wait for 3 seconds
sleep 3
#Print the second part of the progress bar
printf "[########## ] 50%% completed.\r"
#Wait for 2 seconds
sleep 2
#Print the third part of the progress bar
printf "[############### ] 75%% completed.\r"
#Wait for 1 second
sleep 1
#Print the last of the progress bar
printf "[####################] 100%% completed.\r"
printf "\n\nTask completed.\n\n"
.
The following output appears after 1 second of executing the script:
The following output appears after 7 seconds of executing the script:
Example 2: Implement the Progress Bar Using the “Pv” Command
The full form of the “pv” command is “pipe viewer”. It is used to monitor the progress of the data that is passed through the pipe and display the progress bar based on the size of the data. This command is not installed by default in the system. Run the following command to install the “pv” command before practicing the script of this example:
You have to select a file of large size that is copied from one location to another location. Create a Bash file with the following script that copies the “test.txt” file from the current location to the “/home/fahmida/temp/” location. The “pv” command is used here to display the progress bar. The progress bar is displayed based on the size of the “test.txt” file.
echo "Copying file from one location to another location."
#Wait for 2 seconds
sleep 2
#Copy the file to the destination
cat test.txt | pv -s $(stat -c%s test.txt) > /home/fahmida/temp/test.txt
echo "File has been copied."
The following output is displayed after completing the execution of the script:
Example 3: Implement the Progress Bar Using the “Dialog” Command
Another way of implementing a progress bar in Bash is using the “dialog” command. This command can be used to display a good looking progress bar in the terminal. Many types of widgets can be displayed using this progress bar. The task of the progress bar that is displayed by this command can be controlled by the Bash script. This progress bar is not installed in the system by default. Run the following command to install this progress bar in the system:
Create a Bash file with the following script that displays a progress bar using the “dialog” command. The task of copying the “/etc/passwd” file into the “/home/fahmida/tempdir” location is displayed using a progress bar. The progress bar is divided into five parts; each part is displayed after 2 seconds. The –title option is used in the “dialog” command to display the title of the progress bar. The –gauge option is used in the “dialog” command to display the progress bar with a height of 10 lines and a width of 100 characters. The “Waiting to complete the task” message is displayed above the progress bar.
#Initialize the counter
current_pos=0
(
#Define an infinite loop
for((;;))
do
cat <<EOF
delimiter
$current_pos
#Show the current counter value
cp /etc/passwd to /home/fahmida/tempdir ( $current_pos%):
delimiter
EOF
#Increment the counter by 20
(( current_pos+=20 ))
#Terminate from the loop when the counter value is more than 100
[ $current_pos -gt 100 ] && break
#Wait for 2 seconds after each increment
sleep 2
done
) | dialog --title "Copying file..." --gauge "Waiting to complete the task" 10 100 0
The following output appears after 6 seconds of executing the script:
The following output appears after 10 seconds of executing the script:
Conclusion
The different ways of developing a progress bar using a Bash script are shown in this tutorial to help the Bash users use the progress bar on their program.