Bash While Loop

In this topic, we have demonstrated how to use while loop statement in Bash Script.

The bash while loop can be defined as a control flow statement which allows executing the given set of commands repeatedly as long as the applied condition evaluates to true. For example, we can either run echo command many times or just read a text file line by line and process the result by using while loop in Bash.

Syntax of Bash While Loop

Bash while loop has the following format:

The above syntax is applicable only if the expression contains a single condition.

If there are multiple conditions to include in the expression, then the syntax of the while loop will be as follows:

The while loop one-liner syntax can be defined as:

There are some key points of 'while loop' statement:

  • The condition is checked before executing the commands.
  • The 'while' loop is also capable of performing all the work as for 'loop' can do.
  • The commands between 'do' and 'done' are repeatedly executed as long as the condition evaluates to true.
  • The arguments for a 'while' loop can be a boolean expression.

How it works

The while loop is a restricted entry loop. It means that the condition is checked before executing the commands of the while loop. If the condition evaluates to true, the set of commands following that condition are executed. Otherwise, the loop is terminated, and the program control is given to the other command following the 'done' statement.

Bash While Loop Examples

Following are some examples of bash while loop:

While Loop with Single Condition

In this example, the while loop is used with a single condition in expression. It is the basic example of while loop which will print series of numbers as per user input:

Example

Output

Bash While Loop

While Loop with Multiple Conditions

Following is an example of while loop with multiple conditions in the expression:

Example

Output

Bash While Loop

Infinite While Loop

An infinite loop is a loop that has no ending or termination. If the condition always evaluates to true, it creates an infinite loop. The loop will execute continuously until it is forcefully stopped using CTRL+C :

Example

We can also write the above script in a single line as:

Output

Bash While Loop

Here, we have used the built-in command (:) which always return true. We can also use the built-in command true to create an infinite loop just as below:

Example

This bash script will also provide the same output as an above infinite script.

Note: Infinite loops can be terminated by using CTRL+C or by adding some conditional exit within the script.

While Loop with a Break Statement

A break statement can be used to stop the loop as per the applied condition. For example:

Example

Output

According to the script, the loop is assigned to iterate for ten times. But there is a condition after eight times of iteration which will break the iteration and terminate the loop. The following output will be shown after executing the script.

Bash While Loop

While Loop with a Continue Statement

A continue statement can be used to skip the iteration for a specific condition inside the while loop.

Example

Output

Bash While Loop

While Loop with C-Style

We can also write while loop in bash script as similar as a while loop in C programming language.

Example

Output

Bash While Loop

Conclusion

In this topic, we discussed how to use while loop statement in Bash to perform specific tasks.


Next TopicBash Until Loop