Software & AppsOperating SystemLinux

How To Create a Progress Bar in Bash

Ubuntu 18

In this article, we will explore how to create a progress bar in Bash. Bash, or the Bourne Again Shell, is a widely used command-line interpreter. It is a powerful tool that allows users to interact with the system using text-based commands and scripts. One of the many things you can do with Bash is creating a progress bar, which can be useful for tracking the progress of a task or operation.

Quick Answer

Creating a progress bar in Bash is possible using various methods such as using a loop and printing a series of characters, using the pv command, or using ANSI escape codes. These methods allow you to visually track the progress of a task or operation in the Bash command-line interpreter.

What is a Progress Bar?

A progress bar is a graphical representation that visualizes the completion of a task or operation. It is a useful feature that provides visual feedback to the user about the status of an ongoing process.

Creating a Progress Bar in Bash

There are several ways to create a progress bar in Bash. We will discuss three methods in this article:

  1. Using a loop and printing a series of characters
  2. Using the pv command
  3. Using ANSI escape codes

Method 1: Using a Loop and Printing a Series of Characters

This method involves using a loop to iterate through a series of elements and printing a series of “#” characters to represent the progress. Here is a sample script:

#!/bin/bash

pass='number1 number12 number13 number14 number15 number16'
chk='number14'
total=$(echo $pass | wc -w)
progress=0

for i in $pass; do
 if [ "$i" == "$chk" ]; then
 echo "Found ^_^"
 else
 ((progress++))
 percentage=$((progress * 100 / total))
 bar=$(printf "%-${percentage}s" "#")
 echo -ne "Loading: [$bar] $percentage%\r"
 sleep 0.1
 fi
done
echo

In this script, pass is a string containing a series of numbers. chk is the number we are looking for. total is the total number of elements in pass, and progress is the current progress.

The script uses a for loop to iterate through each element in pass. If the current element matches chk, it prints “Found ^_^”. Otherwise, it increments progress and calculates the percentage completed (percentage). It then uses the printf function to print a series of “#” characters (bar) equal to the percentage completed, and updates the progress bar using the echo command with the -ne options.

Method 2: Using the pv Command

The pv command, or Pipe Viewer, is a terminal-based tool for monitoring the progress of data through a pipeline. It can be used to create a progress bar in Bash. Here is a sample script:

#!/bin/bash

pass='number1 number12 number13 number14 number15 number16'
chk='number14'
total=$(echo $pass | wc -w)
progress=0

for i in $pass; do
 if [ "$i" == "$chk" ]; then
 echo "Found ^_^"
 else
 ((progress++))
 percentage=$((progress * 100 / total))
 echo $percentage | pv -qL 10
 sleep 0.1
 fi
done

In this script, we calculate the percentage completed and pass it to pv with the -qL options. The -q option tells pv to not display any information other than the progress bar, and the -L option limits the rate at which pv transfers data.

Method 3: Using ANSI Escape Codes

ANSI escape codes are a standard for in-band signaling to control the cursor location, color, and other options on video text terminals. They can be used to create a progress bar in Bash. Here is a sample script:

#!/bin/bash

pass='number1 number12 number13 number14 number15 number16'
chk='number14'
total=$(echo $pass | wc -w)
progress=0

for i in $pass; do
 if [ "$i" == "$chk" ]; then
 echo "Found ^_^"
 else
 ((progress++))
 percentage=$((progress * 100 / total))
 bar_length=$((percentage / 2))
 bar=$(printf "%-${bar_length}s" "#")
 echo -ne "Loading: [$bar] $percentage%\r"
 sleep 0.1
 fi
done
echo

This script uses ANSI escape codes to update the progress bar. It calculates the percentage completed and determines the length of the progress bar (bar_length) by dividing the percentage by 2. It then prints a series of “#” characters to represent the progress.

Conclusion

Creating a progress bar in Bash can be accomplished in several ways. Depending on your specific requirements, you can choose the approach that best suits your needs. Whether you’re using a loop and printing a series of characters, the pv command, or ANSI escape codes, you can create a progress bar that provides visual feedback to the user about the status of an ongoing process.

How can I run a Bash script?

To run a Bash script, you can open a terminal or command prompt and navigate to the directory where the script is located. Then, you can use the bash command followed by the name of the script file. For example, if your script is named script.sh, you can run it by typing bash script.sh in the terminal.

How do I check the version of Bash installed on my system?

You can check the version of Bash installed on your system by opening a terminal and typing bash --version. This command will display the version number of Bash.

Can I use variables in a Bash script?

Yes, you can use variables in a Bash script. Variables are used to store and manipulate data. To assign a value to a variable, you can use the syntax variable_name=value. For example, name="John" assigns the value "John" to the variable name. You can then use the variable by referencing its name preceded by a dollar sign, such as echo $name to display the value.

How can I pass arguments to a Bash script?

You can pass arguments to a Bash script when running it. The arguments can be accessed within the script using special variables. The first argument is stored in $1, the second in $2, and so on. You can access all the arguments passed using $@ or $*. For example, if you run bash script.sh argument1 argument2, $1 will contain "argument1" and $2 will contain "argument2".

How can I redirect the output of a Bash script to a file?

You can redirect the output of a Bash script to a file using the > symbol. For example, to redirect the output to a file named output.txt, you can use bash script.sh > output.txt. This will save the output of the script to the specified file instead of displaying it in the terminal.

How can I make a Bash script executable?

To make a Bash script executable, you need to set the executable permission on the file. You can use the chmod command followed by the permission settings. For example, chmod +x script.sh will make the script executable. After setting the permission, you can run the script directly without using the bash command by typing ./script.sh in the terminal.

Can I use conditional statements in Bash scripts?

Yes, you can use conditional statements in Bash scripts. Conditional statements allow you to perform different actions based on certain conditions. The syntax for a basic conditional statement is:

if [ condition ]; then
 # code to execute if condition is true
else
 # code to execute if condition is false
fi

You can use various comparison operators like -eq, -ne, -lt, -gt, -le, -ge, and logical operators like && (AND), || (OR), and ! (NOT) to form conditions.

How can I use loops in Bash scripts?

You can use loops in Bash scripts to repeat a set of instructions multiple times. The for loop is commonly used in Bash. Here’s an example of a for loop:

for variable in value1 value2 ... valueN
do
 # code to execute for each value
done

The loop will iterate over the specified values, and the code within the loop will be executed for each value. Another common loop is the while loop, which executes a block of code as long as a specified condition is true.

Leave a Comment

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